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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 StreamEventTypes = require("./service/RTC/StreamEventTypes");
  7. var RTCEvents = require("./service/RTC/RTCEvents");
  8. var EventEmitter = require("events");
  9. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  10. var JitsiConferenceErrors = require("./JitsiConferenceErrors");
  11. var JitsiParticipant = require("./JitsiParticipant");
  12. var Statistics = require("./modules/statistics/statistics");
  13. var JitsiDTMFManager = require('./modules/DTMF/JitsiDTMFManager');
  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(!options.config.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(send by sendPresenceCommand method).
  98. * @param command {String} the name of the command
  99. * @param handler {Function} handler for the command
  100. */
  101. JitsiConference.prototype.addCommandListener = function (command, handler) {
  102. if(this.room)
  103. this.room.addPresenceListener(command, handler);
  104. };
  105. /**
  106. * Removes command listener
  107. * @param command {String} the name of the command
  108. */
  109. JitsiConference.prototype.removeCommandListener = function (command) {
  110. if(this.room)
  111. this.room.removePresenceListener(command);
  112. };
  113. /**
  114. * Sends text message to the other participants in the conference
  115. * @param message the text message.
  116. */
  117. JitsiConference.prototype.sendTextMessage = function (message) {
  118. if(this.room)
  119. this.room.sendMessage(message);
  120. };
  121. /**
  122. * Send presence command.
  123. * @param name the name of the command.
  124. * @param values Object with keys and values that will be send.
  125. **/
  126. JitsiConference.prototype.sendCommand = function (name, values) {
  127. if(this.room) {
  128. this.room.addToPresence(name, values);
  129. this.room.sendPresence();
  130. }
  131. };
  132. /**
  133. * Send presence command one time.
  134. * @param name the name of the command.
  135. * @param values Object with keys and values that will be send.
  136. **/
  137. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  138. this.sendCommand(name, values);
  139. this.removeCommand(name);
  140. };
  141. /**
  142. * Send presence command.
  143. * @param name the name of the command.
  144. * @param values Object with keys and values that will be send.
  145. * @param persistent if false the command will be sent only one time
  146. **/
  147. JitsiConference.prototype.removeCommand = function (name) {
  148. if(this.room)
  149. this.room.removeFromPresence(name);
  150. };
  151. /**
  152. * Sets the display name for this conference.
  153. * @param name the display name to set
  154. */
  155. JitsiConference.prototype.setDisplayName = function(name) {
  156. if(this.room){
  157. this.room.addToPresence("nick", {
  158. attributes: {
  159. xmlns: 'http://jabber.org/protocol/nick'
  160. }, value: name
  161. });
  162. this.room.sendPresence();
  163. }
  164. };
  165. /**
  166. * Adds JitsiLocalTrack object to the conference.
  167. * @param track the JitsiLocalTrack object.
  168. */
  169. JitsiConference.prototype.addTrack = function (track) {
  170. this.rtc.addLocalStream(track);
  171. this.room.addStream(track.getOriginalStream(), function () {});
  172. };
  173. /**
  174. * Removes JitsiLocalTrack object to the conference.
  175. * @param track the JitsiLocalTrack object.
  176. */
  177. JitsiConference.prototype.removeTrack = function (track) {
  178. this.room.removeStream(track.getOriginalStream());
  179. this.rtc.removeLocalStream(track);
  180. };
  181. /**
  182. * Get role of the local user.
  183. * @returns {string} user role: 'moderator' or 'none'
  184. */
  185. JitsiConference.prototype.getRole = function () {
  186. return this.room.role;
  187. };
  188. /**
  189. * Check if local user is moderator.
  190. * @returns {boolean} true if local user is moderator, false otherwise.
  191. */
  192. JitsiConference.prototype.isModerator = function () {
  193. return this.room.isModerator();
  194. };
  195. /**
  196. * Set password for the room.
  197. * @param {string} password new password for the room.
  198. * @returns {Promise}
  199. */
  200. JitsiConference.prototype.lock = function (password) {
  201. if (!this.isModerator()) {
  202. return Promise.reject();
  203. }
  204. var conference = this;
  205. return new Promise(function (resolve, reject) {
  206. conference.xmpp.lockRoom(password, function () {
  207. resolve();
  208. }, function (err) {
  209. reject(err);
  210. }, function () {
  211. reject(JitsiConferenceErrors.PASSWORD_REQUIRED);
  212. });
  213. });
  214. };
  215. /**
  216. * Remove password from the room.
  217. * @returns {Promise}
  218. */
  219. JitsiConference.prototype.unlock = function () {
  220. return this.lock(undefined);
  221. };
  222. /**
  223. * Elects the participant with the given id to be the selected participant or the speaker.
  224. * @param id the identifier of the participant
  225. */
  226. JitsiConference.prototype.selectParticipant = function(participantId) {
  227. if (this.rtc) {
  228. this.rtc.selectedEndpoint(participantId);
  229. }
  230. };
  231. /**
  232. *
  233. * @param id the identifier of the participant
  234. */
  235. JitsiConference.prototype.pinParticipant = function(participantId) {
  236. if (this.rtc) {
  237. this.rtc.pinEndpoint(participantId);
  238. }
  239. };
  240. /**
  241. * Returns the list of participants for this conference.
  242. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  243. */
  244. JitsiConference.prototype.getParticipants = function() {
  245. return Object.keys(this.participants).map(function (key) {
  246. return this.participants[key];
  247. }, this);
  248. };
  249. /**
  250. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  251. * undefined if there isn't one).
  252. * @param id the id of the participant.
  253. */
  254. JitsiConference.prototype.getParticipantById = function(id) {
  255. return this.participants[id];
  256. };
  257. JitsiConference.prototype.onMemberJoined = function (jid, email, nick) {
  258. var id = Strophe.getResourceFromJid(jid);
  259. var participant = new JitsiParticipant(id, this, nick);
  260. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id);
  261. this.participants[id] = participant;
  262. this.xmpp.connection.disco.info(
  263. jid, "node", function(iq) {
  264. participant._supportsDTMF = $(iq).find(
  265. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  266. this.updateDTMFSupport();
  267. }.bind(this)
  268. );
  269. };
  270. JitsiConference.prototype.onMemberLeft = function (jid) {
  271. var id = Strophe.getResourceFromJid(jid);
  272. delete this.participants[id];
  273. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id);
  274. };
  275. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  276. var id = Strophe.getResourceFromJid(jid);
  277. var participant = this.getParticipantById(id);
  278. if (!participant) {
  279. return;
  280. }
  281. participant._role = role;
  282. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  283. };
  284. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  285. var id = Strophe.getResourceFromJid(jid);
  286. var participant = this.getParticipantById(id);
  287. if (!participant) {
  288. return;
  289. }
  290. participant._displayName = displayName;
  291. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  292. };
  293. JitsiConference.prototype.onTrackAdded = function (track) {
  294. var id = track.getParticipantId();
  295. var participant = this.getParticipantById(id);
  296. participant._tracks.push(track);
  297. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  298. };
  299. JitsiConference.prototype.onTrackRemoved = function (track) {
  300. var id = track.getParticipantId();
  301. var participant = this.getParticipantById(id);
  302. var pos = participant._tracks.indexOf(track);
  303. if (pos > -1) {
  304. participant._tracks.splice(pos, 1);
  305. }
  306. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  307. };
  308. JitsiConference.prototype.updateDTMFSupport = function () {
  309. var somebodySupportsDTMF = false;
  310. var participants = this.getParticipants();
  311. // check if at least 1 participant supports DTMF
  312. for (var i = 0; i < participants.length; i += 1) {
  313. if (participants[i].supportsDTMF()) {
  314. somebodySupportsDTMF = true;
  315. break;
  316. }
  317. }
  318. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  319. this.somebodySupportsDTMF = somebodySupportsDTMF;
  320. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  321. }
  322. };
  323. /**
  324. * Allows to check if there is at least one user in the conference
  325. * that supports DTMF.
  326. * @returns {boolean} true if somebody supports DTMF, false otherwise
  327. */
  328. JitsiConference.prototype.isDTMFSupported = function () {
  329. return this.somebodySupportsDTMF;
  330. };
  331. /**
  332. * Returns the local user's ID
  333. * @return {string} local user's ID
  334. */
  335. JitsiConference.prototype.myUserId = function () {
  336. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  337. };
  338. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  339. if (!this.dtmfManager) {
  340. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  341. if (!connection) {
  342. logger.warn("cannot sendTones: no conneciton");
  343. return;
  344. }
  345. var tracks = this.getLocalTracks().filter(function (track) {
  346. return track.isAudioTrack();
  347. });
  348. if (!tracks.length) {
  349. logger.warn("cannot sendTones: no local audio stream");
  350. return;
  351. }
  352. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  353. }
  354. this.dtmfManager.sendTones(tones, duration, pause);
  355. };
  356. /**
  357. * Setups the listeners needed for the conference.
  358. * @param conference the conference
  359. */
  360. function setupListeners(conference) {
  361. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  362. conference.rtc.onIncommingCall(event);
  363. if(conference.statistics)
  364. conference.statistics.startRemoteStats(event.peerconnection);
  365. });
  366. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  367. conference.rtc.createRemoteStream.bind(conference.rtc));
  368. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  369. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  370. });
  371. // FIXME
  372. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  373. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  374. // });
  375. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  376. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  377. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  378. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  379. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  380. });
  381. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  382. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  383. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  384. });
  385. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  386. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  387. });
  388. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  389. conference.eventEmitter.emit(JitsiConferenceEvents.SETUP_FAILED);
  390. });
  391. conference.rtc.addListener(
  392. StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, conference.onTrackAdded.bind(conference)
  393. );
  394. //FIXME: Maybe remove event should not be associated with the conference.
  395. conference.rtc.addListener(
  396. StreamEventTypes.EVENT_TYPE_REMOTE_ENDED, conference.onTrackRemoved.bind(conference)
  397. );
  398. //FIXME: Maybe remove event should not be associated with the conference.
  399. conference.rtc.addListener(StreamEventTypes.EVENT_TYPE_LOCAL_ENDED, function (stream) {
  400. conference.removeTrack(stream);
  401. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, stream);
  402. });
  403. conference.rtc.addListener(StreamEventTypes.TRACK_MUTE_CHANGED, function (track) {
  404. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  405. });
  406. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  407. if(conference.lastActiveSpeaker !== id && conference.room) {
  408. conference.lastActiveSpeaker = id;
  409. conference.eventEmitter.emit(JitsiConferenceEvents.ACTIVE_SPEAKER_CHANGED, id);
  410. }
  411. });
  412. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  413. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  414. });
  415. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  416. function (lastNEndpoints, endpointsEnteringLastN) {
  417. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  418. lastNEndpoints, endpointsEnteringLastN);
  419. });
  420. conference.xmpp.addListener(XMPPEvents.PASSWORD_REQUIRED, function () {
  421. conference.eventEmitter.emit(JitsiConferenceErrors.PASSWORD_REQUIRED);
  422. });
  423. if(conference.statistics) {
  424. //FIXME: Maybe remove event should not be associated with the conference.
  425. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  426. var userId = null;
  427. if (ssrc === Statistics.LOCAL_JID) {
  428. userId = conference.myUserId();
  429. } else {
  430. var jid = conference.room.getJidBySSRC(ssrc);
  431. if (!jid)
  432. return;
  433. userId = Strophe.getResourceFromJid(jid);
  434. }
  435. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  436. userId, level);
  437. });
  438. conference.rtc.addListener(StreamEventTypes.EVENT_TYPE_LOCAL_CREATED, function (stream) {
  439. conference.statistics.startLocalStats(stream);
  440. });
  441. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  442. function () {
  443. conference.statistics.dispose();
  444. });
  445. // FIXME: Maybe we should move this.
  446. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  447. // conference.room.updateDeviceAvailability(devices);
  448. // });
  449. }
  450. }
  451. module.exports = JitsiConference;