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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. /* global Strophe, $, Promise */
  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 AuthenticationEvents = require("./service/authentication/AuthenticationEvents");
  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. var JitsiTrackEvents = require("./JitsiTrackEvents");
  15. var Settings = require("./modules/settings/Settings");
  16. /**
  17. * Creates a JitsiConference object with the given name and properties.
  18. * Note: this constructor is not a part of the public API (objects should be
  19. * created using JitsiConnection.createConference).
  20. * @param options.config properties / settings related to the conference that will be created.
  21. * @param options.name the name of the conference
  22. * @param options.connection the JitsiConnection object for this JitsiConference.
  23. * @constructor
  24. */
  25. function JitsiConference(options) {
  26. if(!options.name || options.name.toLowerCase() !== options.name) {
  27. logger.error("Invalid conference name (no conference name passed or it"
  28. + "contains invalid characters like capital letters)!");
  29. return;
  30. }
  31. this.options = options;
  32. this.connection = this.options.connection;
  33. this.xmpp = this.connection.xmpp;
  34. this.eventEmitter = new EventEmitter();
  35. var confID = this.options.name + '@' + this.xmpp.options.hosts.muc;
  36. this.settings = new Settings(confID);
  37. this.room = this.xmpp.createRoom(this.options.name, this.options.config,
  38. this.settings);
  39. this.room.updateDeviceAvailability(RTC.getDeviceAvailability());
  40. this.rtc = new RTC(this.room, options);
  41. this.statistics = new Statistics(this.xmpp, {
  42. callStatsID: this.options.config.callStatsID,
  43. callStatsSecret: this.options.config.callStatsSecret,
  44. disableThirdPartyRequests: this.options.config.disableThirdPartyRequests
  45. });
  46. setupListeners(this);
  47. JitsiMeetJS._gumFailedHandler.push(function(error) {
  48. this.statistics.sendGetUserMediaFailed(error);
  49. }.bind(this));
  50. this.participants = {};
  51. this.lastDominantSpeaker = null;
  52. this.dtmfManager = null;
  53. this.somebodySupportsDTMF = false;
  54. this.authEnabled = false;
  55. this.authIdentity;
  56. this.startAudioMuted = false;
  57. this.startVideoMuted = false;
  58. this.startMutedPolicy = {audio: false, video: false};
  59. this.availableDevices = {
  60. audio: undefined,
  61. video: undefined
  62. };
  63. this.isMutedByFocus = false;
  64. }
  65. /**
  66. * Joins the conference.
  67. * @param password {string} the password
  68. */
  69. JitsiConference.prototype.join = function (password) {
  70. if(this.room)
  71. this.room.join(password);
  72. };
  73. /**
  74. * Check if joined to the conference.
  75. */
  76. JitsiConference.prototype.isJoined = function () {
  77. return this.room && this.room.joined;
  78. };
  79. /**
  80. * Leaves the conference.
  81. */
  82. JitsiConference.prototype.leave = function () {
  83. if(this.xmpp && this.room)
  84. this.xmpp.leaveRoom(this.room.roomjid);
  85. this.room = null;
  86. };
  87. /**
  88. * Returns name of this conference.
  89. */
  90. JitsiConference.prototype.getName = function () {
  91. return this.options.name;
  92. };
  93. /**
  94. * Check if authentication is enabled for this conference.
  95. */
  96. JitsiConference.prototype.isAuthEnabled = function () {
  97. return this.authEnabled;
  98. };
  99. /**
  100. * Check if user is logged in.
  101. */
  102. JitsiConference.prototype.isLoggedIn = function () {
  103. return !!this.authIdentity;
  104. };
  105. /**
  106. * Get authorized login.
  107. */
  108. JitsiConference.prototype.getAuthLogin = function () {
  109. return this.authIdentity;
  110. };
  111. /**
  112. * Check if external authentication is enabled for this conference.
  113. */
  114. JitsiConference.prototype.isExternalAuthEnabled = function () {
  115. return this.room && this.room.moderator.isExternalAuthEnabled();
  116. };
  117. /**
  118. * Get url for external authentication.
  119. * @param {boolean} [urlForPopup] if true then return url for login popup,
  120. * else url of login page.
  121. * @returns {Promise}
  122. */
  123. JitsiConference.prototype.getExternalAuthUrl = function (urlForPopup) {
  124. return new Promise(function (resolve, reject) {
  125. if (!this.isExternalAuthEnabled()) {
  126. reject();
  127. return;
  128. }
  129. if (urlForPopup) {
  130. this.room.moderator.getPopupLoginUrl(resolve, reject);
  131. } else {
  132. this.room.moderator.getLoginUrl(resolve, reject);
  133. }
  134. }.bind(this));
  135. };
  136. /**
  137. * Returns the local tracks.
  138. */
  139. JitsiConference.prototype.getLocalTracks = function () {
  140. if (this.rtc) {
  141. return this.rtc.localStreams;
  142. } else {
  143. return [];
  144. }
  145. };
  146. /**
  147. * Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
  148. * in JitsiConferenceEvents.
  149. * @param eventId the event ID.
  150. * @param handler handler for the event.
  151. *
  152. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  153. */
  154. JitsiConference.prototype.on = function (eventId, handler) {
  155. if(this.eventEmitter)
  156. this.eventEmitter.on(eventId, handler);
  157. };
  158. /**
  159. * Removes event listener
  160. * @param eventId the event ID.
  161. * @param [handler] optional, the specific handler to unbind
  162. *
  163. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  164. */
  165. JitsiConference.prototype.off = function (eventId, handler) {
  166. if(this.eventEmitter)
  167. this.eventEmitter.removeListener(eventId, handler);
  168. };
  169. // Common aliases for event emitter
  170. JitsiConference.prototype.addEventListener = JitsiConference.prototype.on;
  171. JitsiConference.prototype.removeEventListener = JitsiConference.prototype.off;
  172. /**
  173. * Receives notifications from another participants for commands / custom events
  174. * (send by sendPresenceCommand method).
  175. * @param command {String} the name of the command
  176. * @param handler {Function} handler for the command
  177. */
  178. JitsiConference.prototype.addCommandListener = function (command, handler) {
  179. if(this.room)
  180. this.room.addPresenceListener(command, handler);
  181. };
  182. /**
  183. * Removes command listener
  184. * @param command {String} the name of the command
  185. */
  186. JitsiConference.prototype.removeCommandListener = function (command) {
  187. if(this.room)
  188. this.room.removePresenceListener(command);
  189. };
  190. /**
  191. * Sends text message to the other participants in the conference
  192. * @param message the text message.
  193. */
  194. JitsiConference.prototype.sendTextMessage = function (message) {
  195. if(this.room)
  196. this.room.sendMessage(message);
  197. };
  198. /**
  199. * Send presence command.
  200. * @param name the name of the command.
  201. * @param values Object with keys and values that will be send.
  202. **/
  203. JitsiConference.prototype.sendCommand = function (name, values) {
  204. if(this.room) {
  205. this.room.addToPresence(name, values);
  206. this.room.sendPresence();
  207. }
  208. };
  209. /**
  210. * Send presence command one time.
  211. * @param name the name of the command.
  212. * @param values Object with keys and values that will be send.
  213. **/
  214. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  215. this.sendCommand(name, values);
  216. this.removeCommand(name);
  217. };
  218. /**
  219. * Send presence command.
  220. * @param name the name of the command.
  221. * @param values Object with keys and values that will be send.
  222. * @param persistent if false the command will be sent only one time
  223. **/
  224. JitsiConference.prototype.removeCommand = function (name) {
  225. if(this.room)
  226. this.room.removeFromPresence(name);
  227. };
  228. /**
  229. * Sets the display name for this conference.
  230. * @param name the display name to set
  231. */
  232. JitsiConference.prototype.setDisplayName = function(name) {
  233. if(this.room){
  234. // remove previously set nickname
  235. this.room.removeFromPresence("nick");
  236. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  237. this.room.sendPresence();
  238. }
  239. };
  240. /**
  241. * Set new subject for this conference. (available only for moderator)
  242. * @param {string} subject new subject
  243. */
  244. JitsiConference.prototype.setSubject = function (subject) {
  245. if (this.room && this.isModerator()) {
  246. this.room.setSubject(subject);
  247. }
  248. };
  249. /**
  250. * Adds JitsiLocalTrack object to the conference.
  251. * @param track the JitsiLocalTrack object.
  252. * @returns {Promise<JitsiLocalTrack>}
  253. * @throws will throw and error if track is video track
  254. * and there is already another video track in the conference.
  255. */
  256. JitsiConference.prototype.addTrack = function (track) {
  257. if (track.isVideoTrack()) {
  258. if (this.rtc.getLocalVideoStream()) {
  259. throw new Error("cannot add second video track to the conference");
  260. }
  261. this.removeCommand("videoType");
  262. this.sendCommand("videoType", {
  263. value: track.videoType,
  264. attributes: {
  265. xmlns: 'http://jitsi.org/jitmeet/video'
  266. }
  267. });
  268. }
  269. track.ssrcHandler = function (conference, ssrcMap) {
  270. if(ssrcMap[this.getMSID()]){
  271. this._setSSRC(ssrcMap[this.getMSID()]);
  272. conference.room.removeListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  273. this.ssrcHandler);
  274. }
  275. }.bind(track, this);
  276. this.room.addListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  277. track.ssrcHandler);
  278. return new Promise(function (resolve) {
  279. this.room.addStream(track.getOriginalStream(), function () {
  280. this.rtc.addLocalStream(track);
  281. if (track.startMuted) {
  282. track.mute();
  283. }
  284. // ensure that we're sharing proper "is muted" state
  285. if (track.isAudioTrack()) {
  286. this.room.setAudioMute(track.isMuted());
  287. } else {
  288. this.room.setVideoMute(track.isMuted());
  289. }
  290. track.muteHandler = this._fireMuteChangeEvent.bind(this, track);
  291. track.stopHandler = this.removeTrack.bind(this, track);
  292. track.audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  293. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
  294. track.muteHandler);
  295. track.addEventListener(JitsiTrackEvents.TRACK_STOPPED,
  296. track.stopHandler);
  297. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  298. track.audioLevelHandler);
  299. //FIXME: This dependacy is not necessary. This is quick fix.
  300. track._setConference(this);
  301. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  302. resolve(track);
  303. }.bind(this));
  304. }.bind(this));
  305. };
  306. /**
  307. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event.
  308. * @param audioLevel the audio level
  309. */
  310. JitsiConference.prototype._fireAudioLevelChangeEvent = function (audioLevel) {
  311. this.eventEmitter.emit(
  312. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  313. this.myUserId(), audioLevel);
  314. };
  315. /**
  316. * Fires TRACK_MUTE_CHANGED change conference event.
  317. * @param track the JitsiTrack object related to the event.
  318. */
  319. JitsiConference.prototype._fireMuteChangeEvent = function (track) {
  320. // check if track was muted by focus and now is unmuted by user
  321. if (this.isMutedByFocus && track.isAudioTrack() && !track.isMuted()) {
  322. this.isMutedByFocus = false;
  323. // unmute local user on server
  324. this.room.muteParticipant(this.room.myroomjid, false);
  325. }
  326. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  327. };
  328. /**
  329. * Removes JitsiLocalTrack object to the conference.
  330. * @param track the JitsiLocalTrack object.
  331. * @returns {Promise}
  332. */
  333. JitsiConference.prototype.removeTrack = function (track) {
  334. if(!this.room){
  335. if(this.rtc) {
  336. this.rtc.removeLocalStream(track);
  337. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  338. }
  339. return Promise.resolve();
  340. }
  341. return new Promise(function (resolve) {
  342. this.room.removeStream(track.getOriginalStream(), function(){
  343. track._setSSRC(null);
  344. //FIXME: This dependacy is not necessary. This is quick fix.
  345. track._setConference(this);
  346. this.rtc.removeLocalStream(track);
  347. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
  348. track.muteHandler);
  349. track.removeEventListener(JitsiTrackEvents.TRACK_STOPPED,
  350. track.stopHandler);
  351. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  352. track.audioLevelHandler);
  353. this.room.removeListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  354. track.ssrcHandler);
  355. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  356. resolve();
  357. }.bind(this), {
  358. mtype: track.getType(),
  359. type: "remove",
  360. ssrc: track.ssrc});
  361. }.bind(this));
  362. };
  363. /**
  364. * Get role of the local user.
  365. * @returns {string} user role: 'moderator' or 'none'
  366. */
  367. JitsiConference.prototype.getRole = function () {
  368. return this.room.role;
  369. };
  370. /**
  371. * Check if local user is moderator.
  372. * @returns {boolean} true if local user is moderator, false otherwise.
  373. */
  374. JitsiConference.prototype.isModerator = function () {
  375. return this.room.isModerator();
  376. };
  377. /**
  378. * Set password for the room.
  379. * @param {string} password new password for the room.
  380. * @returns {Promise}
  381. */
  382. JitsiConference.prototype.lock = function (password) {
  383. if (!this.isModerator()) {
  384. return Promise.reject();
  385. }
  386. var conference = this;
  387. return new Promise(function (resolve, reject) {
  388. conference.room.lockRoom(password || "", function () {
  389. resolve();
  390. }, function (err) {
  391. reject(err);
  392. }, function () {
  393. reject(JitsiConferenceErrors.PASSWORD_NOT_SUPPORTED);
  394. });
  395. });
  396. };
  397. /**
  398. * Remove password from the room.
  399. * @returns {Promise}
  400. */
  401. JitsiConference.prototype.unlock = function () {
  402. return this.lock();
  403. };
  404. /**
  405. * Elects the participant with the given id to be the selected participant or the speaker.
  406. * @param id the identifier of the participant
  407. */
  408. JitsiConference.prototype.selectParticipant = function(participantId) {
  409. if (this.rtc) {
  410. this.rtc.selectedEndpoint(participantId);
  411. }
  412. };
  413. /**
  414. *
  415. * @param id the identifier of the participant
  416. */
  417. JitsiConference.prototype.pinParticipant = function(participantId) {
  418. if (this.rtc) {
  419. this.rtc.pinEndpoint(participantId);
  420. }
  421. };
  422. /**
  423. * Returns the list of participants for this conference.
  424. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  425. */
  426. JitsiConference.prototype.getParticipants = function() {
  427. return Object.keys(this.participants).map(function (key) {
  428. return this.participants[key];
  429. }, this);
  430. };
  431. /**
  432. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  433. * undefined if there isn't one).
  434. * @param id the id of the participant.
  435. */
  436. JitsiConference.prototype.getParticipantById = function(id) {
  437. return this.participants[id];
  438. };
  439. /**
  440. * Kick participant from this conference.
  441. * @param {string} id id of the participant to kick
  442. */
  443. JitsiConference.prototype.kickParticipant = function (id) {
  444. var participant = this.getParticipantById(id);
  445. if (!participant) {
  446. return;
  447. }
  448. this.room.kick(participant.getJid());
  449. };
  450. /**
  451. * Kick participant from this conference.
  452. * @param {string} id id of the participant to kick
  453. */
  454. JitsiConference.prototype.muteParticipant = function (id) {
  455. var participant = this.getParticipantById(id);
  456. if (!participant) {
  457. return;
  458. }
  459. this.room.muteParticipant(participant.getJid(), true);
  460. };
  461. JitsiConference.prototype.onMemberJoined = function (jid, nick, role) {
  462. var id = Strophe.getResourceFromJid(jid);
  463. if (id === 'focus' || this.myUserId() === id) {
  464. return;
  465. }
  466. var participant = new JitsiParticipant(jid, this, nick);
  467. participant._role = role;
  468. this.participants[id] = participant;
  469. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id, participant);
  470. this.xmpp.connection.disco.info(
  471. jid, "node", function(iq) {
  472. participant._supportsDTMF = $(iq).find(
  473. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  474. this.updateDTMFSupport();
  475. }.bind(this)
  476. );
  477. };
  478. JitsiConference.prototype.onMemberLeft = function (jid) {
  479. var id = Strophe.getResourceFromJid(jid);
  480. if (id === 'focus' || this.myUserId() === id) {
  481. return;
  482. }
  483. var participant = this.participants[id];
  484. delete this.participants[id];
  485. this.rtc.removeRemoteStream(id);
  486. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  487. };
  488. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  489. var id = Strophe.getResourceFromJid(jid);
  490. var participant = this.getParticipantById(id);
  491. if (!participant) {
  492. return;
  493. }
  494. participant._role = role;
  495. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  496. };
  497. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  498. var id = Strophe.getResourceFromJid(jid);
  499. var participant = this.getParticipantById(id);
  500. if (!participant) {
  501. return;
  502. }
  503. participant._displayName = displayName;
  504. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  505. };
  506. JitsiConference.prototype.onTrackAdded = function (track) {
  507. var id = track.getParticipantId();
  508. var participant = this.getParticipantById(id);
  509. if (!participant) {
  510. return;
  511. }
  512. // add track to JitsiParticipant
  513. participant._tracks.push(track);
  514. var emitter = this.eventEmitter;
  515. track.addEventListener(
  516. JitsiTrackEvents.TRACK_STOPPED,
  517. function () {
  518. // remove track from JitsiParticipant
  519. var pos = participant._tracks.indexOf(track);
  520. if (pos > -1) {
  521. participant._tracks.splice(pos, 1);
  522. }
  523. emitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  524. }
  525. );
  526. track.addEventListener(
  527. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  528. function () {
  529. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  530. }
  531. );
  532. track.addEventListener(
  533. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  534. function (audioLevel) {
  535. emitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  536. }
  537. );
  538. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  539. };
  540. JitsiConference.prototype.updateDTMFSupport = function () {
  541. var somebodySupportsDTMF = false;
  542. var participants = this.getParticipants();
  543. // check if at least 1 participant supports DTMF
  544. for (var i = 0; i < participants.length; i += 1) {
  545. if (participants[i].supportsDTMF()) {
  546. somebodySupportsDTMF = true;
  547. break;
  548. }
  549. }
  550. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  551. this.somebodySupportsDTMF = somebodySupportsDTMF;
  552. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  553. }
  554. };
  555. /**
  556. * Allows to check if there is at least one user in the conference
  557. * that supports DTMF.
  558. * @returns {boolean} true if somebody supports DTMF, false otherwise
  559. */
  560. JitsiConference.prototype.isDTMFSupported = function () {
  561. return this.somebodySupportsDTMF;
  562. };
  563. /**
  564. * Returns the local user's ID
  565. * @return {string} local user's ID
  566. */
  567. JitsiConference.prototype.myUserId = function () {
  568. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  569. };
  570. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  571. if (!this.dtmfManager) {
  572. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  573. if (!connection) {
  574. logger.warn("cannot sendTones: no conneciton");
  575. return;
  576. }
  577. var tracks = this.getLocalTracks().filter(function (track) {
  578. return track.isAudioTrack();
  579. });
  580. if (!tracks.length) {
  581. logger.warn("cannot sendTones: no local audio stream");
  582. return;
  583. }
  584. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  585. }
  586. this.dtmfManager.sendTones(tones, duration, pause);
  587. };
  588. /**
  589. * Returns true if the recording is supproted and false if not.
  590. */
  591. JitsiConference.prototype.isRecordingSupported = function () {
  592. if(this.room)
  593. return this.room.isRecordingSupported();
  594. return false;
  595. };
  596. /**
  597. * Returns null if the recording is not supported, "on" if the recording started
  598. * and "off" if the recording is not started.
  599. */
  600. JitsiConference.prototype.getRecordingState = function () {
  601. if(this.room)
  602. return this.room.getRecordingState();
  603. return "off";
  604. }
  605. /**
  606. * Returns the url of the recorded video.
  607. */
  608. JitsiConference.prototype.getRecordingURL = function () {
  609. if(this.room)
  610. return this.room.getRecordingURL();
  611. return null;
  612. }
  613. /**
  614. * Starts/stops the recording
  615. */
  616. JitsiConference.prototype.toggleRecording = function (options) {
  617. if(this.room)
  618. return this.room.toggleRecording(options, function (status, error) {
  619. this.eventEmitter.emit(
  620. JitsiConferenceEvents.RECORDING_STATE_CHANGED, status, error);
  621. }.bind(this));
  622. this.eventEmitter.emit(
  623. JitsiConferenceEvents.RECORDING_STATE_CHANGED, "error",
  624. new Error("The conference is not created yet!"));
  625. }
  626. /**
  627. * Returns true if the SIP calls are supported and false otherwise
  628. */
  629. JitsiConference.prototype.isSIPCallingSupported = function () {
  630. if(this.room)
  631. return this.room.isSIPCallingSupported();
  632. return false;
  633. }
  634. /**
  635. * Dials a number.
  636. * @param number the number
  637. */
  638. JitsiConference.prototype.dial = function (number) {
  639. if(this.room)
  640. return this.room.dial(number);
  641. return new Promise(function(resolve, reject){
  642. reject(new Error("The conference is not created yet!"))});
  643. }
  644. /**
  645. * Hangup an existing call
  646. */
  647. JitsiConference.prototype.hangup = function () {
  648. if(this.room)
  649. return this.room.hangup();
  650. return new Promise(function(resolve, reject){
  651. reject(new Error("The conference is not created yet!"))});
  652. }
  653. /**
  654. * Returns the phone number for joining the conference.
  655. */
  656. JitsiConference.prototype.getPhoneNumber = function () {
  657. if(this.room)
  658. return this.room.getPhoneNumber();
  659. return null;
  660. }
  661. /**
  662. * Returns the pin for joining the conference with phone.
  663. */
  664. JitsiConference.prototype.getPhonePin = function () {
  665. if(this.room)
  666. return this.room.getPhonePin();
  667. return null;
  668. }
  669. /**
  670. * Returns the connection state for the current room. Its ice connection state
  671. * for its session.
  672. */
  673. JitsiConference.prototype.getConnectionState = function () {
  674. if(this.room)
  675. return this.room.getConnectionState();
  676. return null;
  677. }
  678. /**
  679. * Make all new participants mute their audio/video on join.
  680. * @param policy {Object} object with 2 boolean properties for video and audio:
  681. * @param {boolean} audio if audio should be muted.
  682. * @param {boolean} video if video should be muted.
  683. */
  684. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  685. if (!this.isModerator()) {
  686. return;
  687. }
  688. this.startMutedPolicy = policy;
  689. this.room.removeFromPresence("startmuted");
  690. this.room.addToPresence("startmuted", {
  691. attributes: {
  692. audio: policy.audio,
  693. video: policy.video,
  694. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  695. }
  696. });
  697. this.room.sendPresence();
  698. };
  699. /**
  700. * Returns current start muted policy
  701. * @returns {Object} with 2 proprties - audio and video.
  702. */
  703. JitsiConference.prototype.getStartMutedPolicy = function () {
  704. return this.startMutedPolicy;
  705. };
  706. /**
  707. * Check if audio is muted on join.
  708. */
  709. JitsiConference.prototype.isStartAudioMuted = function () {
  710. return this.startAudioMuted;
  711. };
  712. /**
  713. * Check if video is muted on join.
  714. */
  715. JitsiConference.prototype.isStartVideoMuted = function () {
  716. return this.startVideoMuted;
  717. };
  718. /**
  719. * Get object with internal logs.
  720. */
  721. JitsiConference.prototype.getLogs = function () {
  722. var data = this.xmpp.getJingleLog();
  723. var metadata = {};
  724. metadata.time = new Date();
  725. metadata.url = window.location.href;
  726. metadata.ua = navigator.userAgent;
  727. var log = this.xmpp.getXmppLog();
  728. if (log) {
  729. metadata.xmpp = log;
  730. }
  731. data.metadata = metadata;
  732. return data;
  733. };
  734. /**
  735. * Sends the given feedback through CallStats if enabled.
  736. *
  737. * @param overallFeedback an integer between 1 and 5 indicating the
  738. * user feedback
  739. * @param detailedFeedback detailed feedback from the user. Not yet used
  740. */
  741. JitsiConference.prototype.sendFeedback =
  742. function(overallFeedback, detailedFeedback){
  743. this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  744. }
  745. /**
  746. * Returns true if the callstats integration is enabled, otherwise returns
  747. * false.
  748. *
  749. * @returns true if the callstats integration is enabled, otherwise returns
  750. * false.
  751. */
  752. JitsiConference.prototype.isCallstatsEnabled = function () {
  753. return this.statistics.isCallstatsEnabled();
  754. }
  755. /**
  756. * Setups the listeners needed for the conference.
  757. * @param conference the conference
  758. */
  759. function setupListeners(conference) {
  760. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  761. conference.rtc.onIncommingCall(event);
  762. conference.statistics.startRemoteStats(event.peerconnection);
  763. });
  764. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  765. function (data, sid, thessrc) {
  766. var track = conference.rtc.createRemoteStream(data, sid, thessrc);
  767. if (track) {
  768. conference.onTrackAdded(track);
  769. }
  770. }
  771. );
  772. conference.rtc.addListener(RTCEvents.FAKE_VIDEO_TRACK_CREATED,
  773. function (track) {
  774. conference.onTrackAdded(track);
  775. }
  776. );
  777. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  778. function (value) {
  779. conference.rtc.setAudioMute(value);
  780. conference.isMutedByFocus = true;
  781. }
  782. );
  783. conference.room.addListener(XMPPEvents.SUBJECT_CHANGED, function (subject) {
  784. conference.eventEmitter.emit(JitsiConferenceEvents.SUBJECT_CHANGED, subject);
  785. });
  786. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  787. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  788. });
  789. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  790. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  791. });
  792. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  793. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  794. });
  795. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  796. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  797. });
  798. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  799. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  800. });
  801. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  802. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  803. });
  804. conference.room.addListener(XMPPEvents.RESERVATION_ERROR, function (code, msg) {
  805. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.RESERVATION_ERROR, code, msg);
  806. });
  807. conference.room.addListener(XMPPEvents.GRACEFUL_SHUTDOWN, function () {
  808. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  809. });
  810. conference.room.addListener(XMPPEvents.JINGLE_FATAL_ERROR, function () {
  811. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.JINGLE_FATAL_ERROR);
  812. });
  813. conference.room.addListener(XMPPEvents.MUC_DESTROYED, function (reason) {
  814. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONFERENCE_DESTROYED, reason);
  815. });
  816. conference.room.addListener(XMPPEvents.CHAT_ERROR_RECEIVED, function (err, msg) {
  817. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_ERROR, JitsiConferenceErrors.CHAT_ERROR, err, msg);
  818. });
  819. conference.room.addListener(XMPPEvents.FOCUS_DISCONNECTED, function (focus, retrySec) {
  820. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_DISCONNECTED, focus, retrySec);
  821. });
  822. conference.room.addListener(XMPPEvents.FOCUS_LEFT, function () {
  823. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_LEFT);
  824. });
  825. // FIXME
  826. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  827. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  828. // });
  829. conference.room.addListener(XMPPEvents.KICKED, function () {
  830. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  831. });
  832. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  833. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  834. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  835. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  836. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  837. });
  838. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  839. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  840. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  841. });
  842. conference.room.addListener(XMPPEvents.RECORDING_STATE_CHANGED,
  843. function () {
  844. conference.eventEmitter.emit(
  845. JitsiConferenceEvents.RECORDING_STATE_CHANGED);
  846. });
  847. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  848. conference.eventEmitter.emit(
  849. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  850. });
  851. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  852. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  853. });
  854. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  855. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.SETUP_FAILED);
  856. });
  857. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  858. conference.authEnabled = authEnabled;
  859. conference.authIdentity = authIdentity;
  860. });
  861. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  862. var id = Strophe.getResourceFromJid(jid);
  863. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  864. });
  865. conference.room.addListener(XMPPEvents.PRESENCE_STATUS, function (jid, status) {
  866. var id = Strophe.getResourceFromJid(jid);
  867. var participant = conference.getParticipantById(id);
  868. if (!participant || participant._status === status) {
  869. return;
  870. }
  871. participant._status = status;
  872. conference.eventEmitter.emit(JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  873. });
  874. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  875. if(conference.lastDominantSpeaker !== id && conference.room) {
  876. conference.lastDominantSpeaker = id;
  877. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  878. }
  879. });
  880. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  881. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  882. });
  883. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  884. function (lastNEndpoints, endpointsEnteringLastN) {
  885. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  886. lastNEndpoints, endpointsEnteringLastN);
  887. });
  888. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  889. function (audioMuted, videoMuted) {
  890. conference.startAudioMuted = audioMuted;
  891. conference.startVideoMuted = videoMuted;
  892. // mute existing local tracks because this is initial mute from
  893. // Jicofo
  894. conference.getLocalTracks().forEach(function (track) {
  895. if (conference.startAudioMuted && track.isAudioTrack()) {
  896. track.mute();
  897. }
  898. if (conference.startVideoMuted && track.isVideoTrack()) {
  899. track.mute();
  900. }
  901. });
  902. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  903. });
  904. conference.room.addPresenceListener("startmuted", function (data, from) {
  905. var isModerator = false;
  906. if (conference.myUserId() === from && conference.isModerator()) {
  907. isModerator = true;
  908. } else {
  909. var participant = conference.getParticipantById(from);
  910. if (participant && participant.isModerator()) {
  911. isModerator = true;
  912. }
  913. }
  914. if (!isModerator) {
  915. return;
  916. }
  917. var startAudioMuted = data.attributes.audio === 'true';
  918. var startVideoMuted = data.attributes.video === 'true';
  919. var updated = false;
  920. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  921. conference.startMutedPolicy.audio = startAudioMuted;
  922. updated = true;
  923. }
  924. if (startVideoMuted !== conference.startMutedPolicy.video) {
  925. conference.startMutedPolicy.video = startVideoMuted;
  926. updated = true;
  927. }
  928. if (updated) {
  929. conference.eventEmitter.emit(
  930. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  931. conference.startMutedPolicy
  932. );
  933. }
  934. });
  935. conference.rtc.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  936. conference.room.updateDeviceAvailability(devices);
  937. });
  938. conference.room.addPresenceListener("devices", function (data, from) {
  939. var isAudioAvailable = false;
  940. var isVideoAvailable = false;
  941. data.children.forEach(function (config) {
  942. if (config.tagName === 'audio') {
  943. isAudioAvailable = config.value === 'true';
  944. }
  945. if (config.tagName === 'video') {
  946. isVideoAvailable = config.value === 'true';
  947. }
  948. });
  949. var availableDevices;
  950. if (conference.myUserId() === from) {
  951. availableDevices = conference.availableDevices;
  952. } else {
  953. var participant = conference.getParticipantById(from);
  954. if (!participant) {
  955. return;
  956. }
  957. availableDevices = participant._availableDevices;
  958. }
  959. var updated = false;
  960. if (availableDevices.audio !== isAudioAvailable) {
  961. updated = true;
  962. availableDevices.audio = isAudioAvailable;
  963. }
  964. if (availableDevices.video !== isVideoAvailable) {
  965. updated = true;
  966. availableDevices.video = isVideoAvailable;
  967. }
  968. if (updated) {
  969. conference.eventEmitter.emit(
  970. JitsiConferenceEvents.AVAILABLE_DEVICES_CHANGED,
  971. from, availableDevices);
  972. }
  973. });
  974. if(conference.statistics) {
  975. //FIXME: Maybe remove event should not be associated with the conference.
  976. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  977. var userId = null;
  978. var resource = conference.rtc.getResourceBySSRC(ssrc);
  979. if (!resource)
  980. return;
  981. conference.rtc.setAudioLevel(resource, level);
  982. });
  983. conference.statistics.addConnectionStatsListener(function (stats) {
  984. var ssrc2resolution = stats.resolution;
  985. var id2resolution = {};
  986. // preprocess resolutions: group by user id, skip incorrect
  987. // resolutions etc.
  988. Object.keys(ssrc2resolution).forEach(function (ssrc) {
  989. var resolution = ssrc2resolution[ssrc];
  990. if (!resolution.width || !resolution.height ||
  991. resolution.width == -1 || resolution.height == -1) {
  992. return;
  993. }
  994. var id = conference.rtc.getResourceBySSRC(ssrc);
  995. if (!id) {
  996. return;
  997. }
  998. // ssrc to resolution map for user id
  999. var idResolutions = id2resolution[id] || {};
  1000. idResolutions[ssrc] = resolution;
  1001. id2resolution[id] = idResolutions;
  1002. });
  1003. stats.resolution = id2resolution;
  1004. conference.eventEmitter.emit(
  1005. JitsiConferenceEvents.CONNECTION_STATS, stats);
  1006. });
  1007. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  1008. function () {
  1009. conference.statistics.dispose();
  1010. });
  1011. conference.room.addListener(XMPPEvents.PEERCONNECTION_READY,
  1012. function (session) {
  1013. conference.statistics.startCallStats(
  1014. session, conference.settings);
  1015. });
  1016. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED,
  1017. function () {
  1018. conference.statistics.sendSetupFailedEvent();
  1019. });
  1020. conference.rtc.addListener(RTCEvents.TRACK_ATTACHED,
  1021. function(track, container) {
  1022. var ssrc = track.getSSRC();
  1023. if (!container.id || !ssrc) {
  1024. return;
  1025. }
  1026. conference.statistics.associateStreamWithVideoTag(
  1027. ssrc, track.isLocal(), track.getUsageLabel(), container.id);
  1028. });
  1029. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  1030. function (track) {
  1031. if(!track.isLocal())
  1032. return;
  1033. var type = (track.getType() === "audio")? "audio" : "video";
  1034. conference.statistics.sendMuteEvent(track.isMuted(), type);
  1035. });
  1036. conference.room.addListener(XMPPEvents.CREATE_OFFER_FAILED, function (e, pc) {
  1037. conference.statistics.sendCreateOfferFailed(e, pc);
  1038. });
  1039. conference.room.addListener(XMPPEvents.CREATE_ANSWER_FAILED, function (e, pc) {
  1040. conference.statistics.sendCreateAnswerFailed(e, pc);
  1041. });
  1042. conference.room.addListener(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  1043. function (e, pc) {
  1044. conference.statistics.sendSetLocalDescFailed(e, pc);
  1045. }
  1046. );
  1047. conference.room.addListener(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  1048. function (e, pc) {
  1049. conference.statistics.sendSetRemoteDescFailed(e, pc);
  1050. }
  1051. );
  1052. conference.room.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  1053. function (e, pc) {
  1054. conference.statistics.sendAddIceCandidateFailed(e, pc);
  1055. }
  1056. );
  1057. }
  1058. }
  1059. module.exports = JitsiConference;