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

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