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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 JitsiParticipant = require("./JitsiParticipant");
  10. var Statistics = require("./modules/statistics/statistics");
  11. var JitsiDTMFManager = require('./modules/DTMF/JitsiDTMFManager');
  12. var JitsiTrackEvents = require("./JitsiTrackEvents");
  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(!RTC.options.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
  97. * (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. // remove previously set nickname
  158. this.room.removeFromPresence("nick");
  159. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  160. this.room.sendPresence();
  161. }
  162. };
  163. /**
  164. * Adds JitsiLocalTrack object to the conference.
  165. * @param track the JitsiLocalTrack object.
  166. */
  167. JitsiConference.prototype.addTrack = function (track) {
  168. this.room.addStream(track.getOriginalStream(), function () {
  169. this.rtc.addLocalStream(track);
  170. var muteHandler = this._fireMuteChangeEvent.bind(this, track);
  171. var stopHandler = this.removeTrack.bind(this, track);
  172. var audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  173. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, muteHandler);
  174. track.addEventListener(JitsiTrackEvents.TRACK_STOPPED, stopHandler);
  175. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, audioLevelHandler);
  176. this.addEventListener(JitsiConferenceEvents.TRACK_REMOVED, function (someTrack) {
  177. if (someTrack !== track) {
  178. return;
  179. }
  180. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, muteHandler);
  181. track.removeEventListener(JitsiTrackEvents.TRACK_STOPPED, stopHandler);
  182. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, audioLevelHandler);
  183. });
  184. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  185. }.bind(this));
  186. };
  187. /**
  188. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event.
  189. * @param audioLevel the audio level
  190. */
  191. JitsiConference.prototype._fireAudioLevelChangeEvent = function (audioLevel) {
  192. this.eventEmitter.emit(
  193. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  194. this.myUserId(), audioLevel);
  195. };
  196. /**
  197. * Fires TRACK_MUTE_CHANGED change conference event.
  198. * @param track the JitsiTrack object related to the event.
  199. */
  200. JitsiConference.prototype._fireMuteChangeEvent = function (track) {
  201. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  202. };
  203. /**
  204. * Removes JitsiLocalTrack object to the conference.
  205. * @param track the JitsiLocalTrack object.
  206. */
  207. JitsiConference.prototype.removeTrack = function (track) {
  208. this.room.removeStream(track.getOriginalStream(), function(){
  209. this.rtc.removeLocalStream(track);
  210. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  211. }.bind(this));
  212. };
  213. /**
  214. * Get role of the local user.
  215. * @returns {string} user role: 'moderator' or 'none'
  216. */
  217. JitsiConference.prototype.getRole = function () {
  218. return this.room.role;
  219. };
  220. /**
  221. * Check if local user is moderator.
  222. * @returns {boolean} true if local user is moderator, false otherwise.
  223. */
  224. JitsiConference.prototype.isModerator = function () {
  225. return this.room.isModerator();
  226. };
  227. /**
  228. * Elects the participant with the given id to be the selected participant or the speaker.
  229. * @param id the identifier of the participant
  230. */
  231. JitsiConference.prototype.selectParticipant = function(participantId) {
  232. if (this.rtc) {
  233. this.rtc.selectedEndpoint(participantId);
  234. }
  235. };
  236. /**
  237. *
  238. * @param id the identifier of the participant
  239. */
  240. JitsiConference.prototype.pinParticipant = function(participantId) {
  241. if (this.rtc) {
  242. this.rtc.pinEndpoint(participantId);
  243. }
  244. };
  245. /**
  246. * Returns the list of participants for this conference.
  247. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  248. */
  249. JitsiConference.prototype.getParticipants = function() {
  250. return Object.keys(this.participants).map(function (key) {
  251. return this.participants[key];
  252. }, this);
  253. };
  254. /**
  255. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  256. * undefined if there isn't one).
  257. * @param id the id of the participant.
  258. */
  259. JitsiConference.prototype.getParticipantById = function(id) {
  260. return this.participants[id];
  261. };
  262. JitsiConference.prototype.onMemberJoined = function (jid, email, nick) {
  263. var id = Strophe.getResourceFromJid(jid);
  264. var participant = new JitsiParticipant(id, this, nick);
  265. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id);
  266. this.participants[id] = participant;
  267. this.connection.xmpp.connection.disco.info(
  268. jid, "node", function(iq) {
  269. participant._supportsDTMF = $(iq).find(
  270. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  271. this.updateDTMFSupport();
  272. }.bind(this)
  273. );
  274. };
  275. JitsiConference.prototype.onMemberLeft = function (jid) {
  276. var id = Strophe.getResourceFromJid(jid);
  277. delete this.participants[id];
  278. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id);
  279. };
  280. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  281. var id = Strophe.getResourceFromJid(jid);
  282. var participant = this.getParticipantById(id);
  283. if (!participant) {
  284. return;
  285. }
  286. participant._role = role;
  287. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  288. };
  289. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  290. var id = Strophe.getResourceFromJid(jid);
  291. var participant = this.getParticipantById(id);
  292. if (!participant) {
  293. return;
  294. }
  295. participant._displayName = displayName;
  296. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  297. };
  298. JitsiConference.prototype.onTrackAdded = function (track) {
  299. var id = track.getParticipantId();
  300. var participant = this.getParticipantById(id);
  301. if (!participant) {
  302. return;
  303. }
  304. // add track to JitsiParticipant
  305. participant._tracks.push(track);
  306. var emitter = this.eventEmitter;
  307. track.addEventListener(
  308. JitsiTrackEvents.TRACK_STOPPED,
  309. function () {
  310. // remove track from JitsiParticipant
  311. var pos = participant._tracks.indexOf(track);
  312. if (pos > -1) {
  313. participant._tracks.splice(pos, 1);
  314. }
  315. emitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  316. }
  317. );
  318. track.addEventListener(
  319. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  320. function () {
  321. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  322. }
  323. );
  324. track.addEventListener(
  325. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  326. function (audioLevel) {
  327. emitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  328. }
  329. );
  330. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  331. };
  332. JitsiConference.prototype.updateDTMFSupport = function () {
  333. var somebodySupportsDTMF = false;
  334. var participants = this.getParticipants();
  335. // check if at least 1 participant supports DTMF
  336. for (var i = 0; i < participants.length; i += 1) {
  337. if (participants[i].supportsDTMF()) {
  338. somebodySupportsDTMF = true;
  339. break;
  340. }
  341. }
  342. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  343. this.somebodySupportsDTMF = somebodySupportsDTMF;
  344. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  345. }
  346. };
  347. /**
  348. * Allows to check if there is at least one user in the conference
  349. * that supports DTMF.
  350. * @returns {boolean} true if somebody supports DTMF, false otherwise
  351. */
  352. JitsiConference.prototype.isDTMFSupported = function () {
  353. return this.somebodySupportsDTMF;
  354. };
  355. /**
  356. * Returns the local user's ID
  357. * @return {string} local user's ID
  358. */
  359. JitsiConference.prototype.myUserId = function () {
  360. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  361. };
  362. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  363. if (!this.dtmfManager) {
  364. var connection = this.connection.xmpp.connection.jingle.activecall.peerconnection;
  365. if (!connection) {
  366. logger.warn("cannot sendTones: no conneciton");
  367. return;
  368. }
  369. var tracks = this.getLocalTracks().filter(function (track) {
  370. return track.isAudioTrack();
  371. });
  372. if (!tracks.length) {
  373. logger.warn("cannot sendTones: no local audio stream");
  374. return;
  375. }
  376. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  377. }
  378. this.dtmfManager.sendTones(tones, duration, pause);
  379. };
  380. /**
  381. * Returns true if the recording is supproted and false if not.
  382. */
  383. JitsiConference.prototype.isRecordingSupported = function () {
  384. if(this.room)
  385. return this.room.isRecordingSupported();
  386. return false;
  387. };
  388. /**
  389. * Returns null if the recording is not supported, "on" if the recording started
  390. * and "off" if the recording is not started.
  391. */
  392. JitsiConference.prototype.getRecordingState = function () {
  393. if(this.room)
  394. return this.room.getRecordingState();
  395. return "off";
  396. }
  397. /**
  398. * Returns the url of the recorded video.
  399. */
  400. JitsiConference.prototype.getRecordingURL = function () {
  401. if(this.room)
  402. return this.room.getRecordingURL();
  403. return null;
  404. }
  405. /**
  406. * Starts/stops the recording
  407. * @param token a token for authentication.
  408. */
  409. JitsiConference.prototype.toggleRecording = function (token, followEntity) {
  410. if(this.room)
  411. return this.room.toggleRecording(token, followEntity);
  412. return new Promise(function(resolve, reject){
  413. reject(new Error("The conference is not created yet!"))});
  414. }
  415. /**
  416. * Dials a number.
  417. * @param number the number
  418. */
  419. JitsiConference.prototype.dial = function (number) {
  420. if(this.room)
  421. return this.room.dial(number);
  422. return new Promise(function(resolve, reject){
  423. reject(new Error("The conference is not created yet!"))});
  424. }
  425. /**
  426. * Hangup an existing call
  427. */
  428. JitsiConference.prototype.hangup = function () {
  429. if(this.room)
  430. return this.room.hangup();
  431. return new Promise(function(resolve, reject){
  432. reject(new Error("The conference is not created yet!"))});
  433. }
  434. /**
  435. * Returns the phone number for joining the conference.
  436. */
  437. JitsiConference.prototype.getPhoneNumber = function () {
  438. if(this.room)
  439. return this.room.getPhoneNumber();
  440. return null;
  441. }
  442. /**
  443. * Returns the pin for joining the conference with phone.
  444. */
  445. JitsiConference.prototype.getPhonePin = function () {
  446. if(this.room)
  447. return this.room.getPhonePin();
  448. return null;
  449. }
  450. /**
  451. * Returns the connection state for the current room. Its ice connection state
  452. * for its session.
  453. */
  454. JitsiConference.prototype.getConnectionState = function () {
  455. if(this.room)
  456. return this.room.getConnectionState();
  457. return null;
  458. }
  459. /**
  460. * Setups the listeners needed for the conference.
  461. * @param conference the conference
  462. */
  463. function setupListeners(conference) {
  464. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  465. conference.rtc.onIncommingCall(event);
  466. if(conference.statistics)
  467. conference.statistics.startRemoteStats(event.peerconnection);
  468. });
  469. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  470. function (data, sid, thessrc) {
  471. var track = conference.rtc.createRemoteStream(data, sid, thessrc);
  472. if (track) {
  473. conference.onTrackAdded(track);
  474. }
  475. }
  476. );
  477. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  478. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  479. });
  480. // FIXME
  481. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  482. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  483. // });
  484. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  485. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  486. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  487. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  488. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  489. });
  490. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  491. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  492. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  493. });
  494. conference.room.addListener(XMPPEvents.RECORDING_STATE_CHANGED,
  495. function () {
  496. conference.eventEmitter.emit(
  497. JitsiConferenceEvents.RECORDING_STATE_CHANGED);
  498. });
  499. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  500. conference.eventEmitter.emit(
  501. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  502. });
  503. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  504. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  505. });
  506. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  507. conference.eventEmitter.emit(JitsiConferenceEvents.SETUP_FAILED);
  508. });
  509. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  510. if(conference.lastActiveSpeaker !== id && conference.room) {
  511. conference.lastActiveSpeaker = id;
  512. conference.eventEmitter.emit(JitsiConferenceEvents.ACTIVE_SPEAKER_CHANGED, id);
  513. }
  514. });
  515. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  516. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  517. });
  518. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  519. function (lastNEndpoints, endpointsEnteringLastN) {
  520. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  521. lastNEndpoints, endpointsEnteringLastN);
  522. });
  523. if(conference.statistics) {
  524. //FIXME: Maybe remove event should not be associated with the conference.
  525. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  526. var userId = null;
  527. var jid = conference.room.getJidBySSRC(ssrc);
  528. if (!jid)
  529. return;
  530. conference.rtc.setAudioLevel(jid, level);
  531. });
  532. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  533. function () {
  534. conference.statistics.dispose();
  535. });
  536. // FIXME: Maybe we should move this.
  537. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  538. // conference.room.updateDeviceAvailability(devices);
  539. // });
  540. }
  541. }
  542. module.exports = JitsiConference;