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

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