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

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