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

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