modified lib-jitsi-meet dev repo
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 40KB

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