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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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. JitsiConference.prototype.onTrackAdded = function (track) {
  573. var id = track.getParticipantId();
  574. var participant = this.getParticipantById(id);
  575. if (!participant) {
  576. return;
  577. }
  578. //FIXME: This is temporary fix
  579. //Make sure that no fake tracks are left.
  580. if(track.isVideoTrack() && !track.isFake) {
  581. //Remove the fake video track
  582. participant._tracks = participant._tracks.filter(function (track) {
  583. return !track.isFake;
  584. });
  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(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  599. }
  600. );
  601. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  602. };
  603. JitsiConference.prototype.updateDTMFSupport = function () {
  604. var somebodySupportsDTMF = false;
  605. var participants = this.getParticipants();
  606. // check if at least 1 participant supports DTMF
  607. for (var i = 0; i < participants.length; i += 1) {
  608. if (participants[i].supportsDTMF()) {
  609. somebodySupportsDTMF = true;
  610. break;
  611. }
  612. }
  613. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  614. this.somebodySupportsDTMF = somebodySupportsDTMF;
  615. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  616. }
  617. };
  618. /**
  619. * Allows to check if there is at least one user in the conference
  620. * that supports DTMF.
  621. * @returns {boolean} true if somebody supports DTMF, false otherwise
  622. */
  623. JitsiConference.prototype.isDTMFSupported = function () {
  624. return this.somebodySupportsDTMF;
  625. };
  626. /**
  627. * Returns the local user's ID
  628. * @return {string} local user's ID
  629. */
  630. JitsiConference.prototype.myUserId = function () {
  631. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  632. };
  633. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  634. if (!this.dtmfManager) {
  635. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  636. if (!connection) {
  637. logger.warn("cannot sendTones: no conneciton");
  638. return;
  639. }
  640. var tracks = this.getLocalTracks().filter(function (track) {
  641. return track.isAudioTrack();
  642. });
  643. if (!tracks.length) {
  644. logger.warn("cannot sendTones: no local audio stream");
  645. return;
  646. }
  647. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  648. }
  649. this.dtmfManager.sendTones(tones, duration, pause);
  650. };
  651. /**
  652. * Returns true if the recording is supproted and false if not.
  653. */
  654. JitsiConference.prototype.isRecordingSupported = function () {
  655. if(this.room)
  656. return this.room.isRecordingSupported();
  657. return false;
  658. };
  659. /**
  660. * Returns null if the recording is not supported, "on" if the recording started
  661. * and "off" if the recording is not started.
  662. */
  663. JitsiConference.prototype.getRecordingState = function () {
  664. return (this.room) ? this.room.getRecordingState() : undefined;
  665. }
  666. /**
  667. * Returns the url of the recorded video.
  668. */
  669. JitsiConference.prototype.getRecordingURL = function () {
  670. return (this.room) ? this.room.getRecordingURL() : null;
  671. }
  672. /**
  673. * Starts/stops the recording
  674. */
  675. JitsiConference.prototype.toggleRecording = function (options) {
  676. if(this.room)
  677. return this.room.toggleRecording(options, function (status, error) {
  678. this.eventEmitter.emit(
  679. JitsiConferenceEvents.RECORDER_STATE_CHANGED, status, error);
  680. }.bind(this));
  681. this.eventEmitter.emit(
  682. JitsiConferenceEvents.RECORDER_STATE_CHANGED, "error",
  683. new Error("The conference is not created yet!"));
  684. }
  685. /**
  686. * Returns true if the SIP calls are supported and false otherwise
  687. */
  688. JitsiConference.prototype.isSIPCallingSupported = function () {
  689. if(this.room)
  690. return this.room.isSIPCallingSupported();
  691. return false;
  692. }
  693. /**
  694. * Dials a number.
  695. * @param number the number
  696. */
  697. JitsiConference.prototype.dial = function (number) {
  698. if(this.room)
  699. return this.room.dial(number);
  700. return new Promise(function(resolve, reject){
  701. reject(new Error("The conference is not created yet!"))});
  702. }
  703. /**
  704. * Hangup an existing call
  705. */
  706. JitsiConference.prototype.hangup = function () {
  707. if(this.room)
  708. return this.room.hangup();
  709. return new Promise(function(resolve, reject){
  710. reject(new Error("The conference is not created yet!"))});
  711. }
  712. /**
  713. * Returns the phone number for joining the conference.
  714. */
  715. JitsiConference.prototype.getPhoneNumber = function () {
  716. if(this.room)
  717. return this.room.getPhoneNumber();
  718. return null;
  719. }
  720. /**
  721. * Returns the pin for joining the conference with phone.
  722. */
  723. JitsiConference.prototype.getPhonePin = function () {
  724. if(this.room)
  725. return this.room.getPhonePin();
  726. return null;
  727. }
  728. /**
  729. * Returns the connection state for the current room. Its ice connection state
  730. * for its session.
  731. */
  732. JitsiConference.prototype.getConnectionState = function () {
  733. if(this.room)
  734. return this.room.getConnectionState();
  735. return null;
  736. }
  737. /**
  738. * Make all new participants mute their audio/video on join.
  739. * @param policy {Object} object with 2 boolean properties for video and audio:
  740. * @param {boolean} audio if audio should be muted.
  741. * @param {boolean} video if video should be muted.
  742. */
  743. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  744. if (!this.isModerator()) {
  745. return;
  746. }
  747. this.startMutedPolicy = policy;
  748. this.room.removeFromPresence("startmuted");
  749. this.room.addToPresence("startmuted", {
  750. attributes: {
  751. audio: policy.audio,
  752. video: policy.video,
  753. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  754. }
  755. });
  756. this.room.sendPresence();
  757. };
  758. /**
  759. * Returns current start muted policy
  760. * @returns {Object} with 2 proprties - audio and video.
  761. */
  762. JitsiConference.prototype.getStartMutedPolicy = function () {
  763. return this.startMutedPolicy;
  764. };
  765. /**
  766. * Check if audio is muted on join.
  767. */
  768. JitsiConference.prototype.isStartAudioMuted = function () {
  769. return this.startAudioMuted;
  770. };
  771. /**
  772. * Check if video is muted on join.
  773. */
  774. JitsiConference.prototype.isStartVideoMuted = function () {
  775. return this.startVideoMuted;
  776. };
  777. /**
  778. * Get object with internal logs.
  779. */
  780. JitsiConference.prototype.getLogs = function () {
  781. var data = this.xmpp.getJingleLog();
  782. var metadata = {};
  783. metadata.time = new Date();
  784. metadata.url = window.location.href;
  785. metadata.ua = navigator.userAgent;
  786. var log = this.xmpp.getXmppLog();
  787. if (log) {
  788. metadata.xmpp = log;
  789. }
  790. data.metadata = metadata;
  791. return data;
  792. };
  793. /**
  794. * Returns measured connectionTimes.
  795. */
  796. JitsiConference.prototype.getConnectionTimes = function () {
  797. return this.room.connectionTimes;
  798. };
  799. /**
  800. * Sends the given feedback through CallStats if enabled.
  801. *
  802. * @param overallFeedback an integer between 1 and 5 indicating the
  803. * user feedback
  804. * @param detailedFeedback detailed feedback from the user. Not yet used
  805. */
  806. JitsiConference.prototype.sendFeedback =
  807. function(overallFeedback, detailedFeedback){
  808. this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  809. }
  810. /**
  811. * Returns true if the callstats integration is enabled, otherwise returns
  812. * false.
  813. *
  814. * @returns true if the callstats integration is enabled, otherwise returns
  815. * false.
  816. */
  817. JitsiConference.prototype.isCallstatsEnabled = function () {
  818. return this.statistics.isCallstatsEnabled();
  819. }
  820. /**
  821. * Setups the listeners needed for the conference.
  822. * @param conference the conference
  823. */
  824. function setupListeners(conference) {
  825. conference.xmpp.addListener(
  826. XMPPEvents.CALL_INCOMING, function (jingleSession, jingleOffer, now) {
  827. if (conference.room.isFocus(jingleSession.peerjid)) {
  828. // Accept incoming call
  829. conference.room.setJingleSession(jingleSession);
  830. conference.room.connectionTimes["session.initiate"] = now;
  831. jingleSession.initialize(false /* initiator */, conference.room);
  832. conference.rtc.onIncommingCall(jingleSession);
  833. jingleSession.acceptOffer(jingleOffer, null,
  834. function (error) {
  835. logger.error(
  836. "Failed to accept incoming Jingle session", error);
  837. }
  838. );
  839. conference.statistics.startRemoteStats(
  840. jingleSession.peerconnection);
  841. } else {
  842. // Error cause this should never happen unless something is wrong !
  843. logger.error(
  844. "Rejecting session-initiate from non focus user: "
  845. + jingleSession.peerjid);
  846. }
  847. });
  848. conference.room.addListener(XMPPEvents.REMOTE_TRACK_ADDED,
  849. function (data) {
  850. var track = conference.rtc.createRemoteTrack(data);
  851. if (track) {
  852. conference.onTrackAdded(track);
  853. }
  854. }
  855. );
  856. conference.room.addListener(XMPPEvents.REMOTE_TRACK_REMOVED,
  857. function (streamId, trackId) {
  858. conference.getParticipants().forEach(function(participant) {
  859. var tracks = participant.getTracks();
  860. for(var i = 0; i < tracks.length; i++) {
  861. if(tracks[i]
  862. && tracks[i].getStreamId() == streamId
  863. && tracks[i].getTrackId() == trackId) {
  864. var track = participant._tracks.splice(i, 1)[0];
  865. conference.eventEmitter.emit(
  866. JitsiConferenceEvents.TRACK_REMOVED, track);
  867. return;
  868. }
  869. }
  870. });
  871. }
  872. );
  873. conference.rtc.addListener(RTCEvents.FAKE_VIDEO_TRACK_CREATED,
  874. function (track) {
  875. conference.onTrackAdded(track);
  876. }
  877. );
  878. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  879. function (value) {
  880. // set isMutedByFocus when setAudioMute Promise ends
  881. conference.rtc.setAudioMute(value).then(
  882. function() {
  883. conference.isMutedByFocus = true;
  884. },
  885. function() {
  886. logger.warn(
  887. "Error while audio muting due to focus request");
  888. });
  889. }
  890. );
  891. conference.room.addListener(XMPPEvents.SUBJECT_CHANGED, function (subject) {
  892. conference.eventEmitter.emit(JitsiConferenceEvents.SUBJECT_CHANGED,
  893. subject);
  894. });
  895. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  896. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  897. });
  898. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  899. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  900. JitsiConferenceErrors.CONNECTION_ERROR, pres);
  901. });
  902. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  903. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  904. JitsiConferenceErrors.CONNECTION_ERROR, pres);
  905. });
  906. conference.room.addListener(XMPPEvents.ROOM_MAX_USERS_ERROR,
  907. function (pres) {
  908. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  909. JitsiConferenceErrors.CONFERENCE_MAX_USERS, pres);
  910. });
  911. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  912. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  913. });
  914. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  915. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  916. });
  917. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  918. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  919. });
  920. conference.room.addListener(XMPPEvents.RESERVATION_ERROR, function (code, msg) {
  921. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.RESERVATION_ERROR, code, msg);
  922. });
  923. conference.room.addListener(XMPPEvents.GRACEFUL_SHUTDOWN, function () {
  924. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  925. });
  926. conference.room.addListener(XMPPEvents.JINGLE_FATAL_ERROR, function () {
  927. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.JINGLE_FATAL_ERROR);
  928. });
  929. conference.room.addListener(XMPPEvents.MUC_DESTROYED, function (reason) {
  930. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONFERENCE_DESTROYED, reason);
  931. });
  932. conference.room.addListener(XMPPEvents.CHAT_ERROR_RECEIVED, function (err, msg) {
  933. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_ERROR, JitsiConferenceErrors.CHAT_ERROR, err, msg);
  934. });
  935. conference.room.addListener(XMPPEvents.FOCUS_DISCONNECTED, function (focus, retrySec) {
  936. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_DISCONNECTED, focus, retrySec);
  937. });
  938. conference.room.addListener(XMPPEvents.FOCUS_LEFT, function () {
  939. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_LEFT);
  940. });
  941. // FIXME
  942. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  943. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  944. // });
  945. conference.room.addListener(XMPPEvents.KICKED, function () {
  946. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  947. });
  948. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  949. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  950. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  951. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  952. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  953. });
  954. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  955. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  956. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  957. });
  958. conference.room.addListener(XMPPEvents.RECORDER_STATE_CHANGED,
  959. function (state) {
  960. conference.eventEmitter.emit(
  961. JitsiConferenceEvents.RECORDER_STATE_CHANGED, state);
  962. });
  963. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  964. conference.eventEmitter.emit(
  965. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  966. });
  967. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  968. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  969. });
  970. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  971. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.SETUP_FAILED);
  972. });
  973. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  974. conference.authEnabled = authEnabled;
  975. conference.authIdentity = authIdentity;
  976. conference.eventEmitter.emit(JitsiConferenceEvents.AUTH_STATUS_CHANGED, authEnabled, authIdentity);
  977. });
  978. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  979. var id = Strophe.getResourceFromJid(jid);
  980. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  981. });
  982. conference.room.addListener(XMPPEvents.PRESENCE_STATUS, function (jid, status) {
  983. var id = Strophe.getResourceFromJid(jid);
  984. var participant = conference.getParticipantById(id);
  985. if (!participant || participant._status === status) {
  986. return;
  987. }
  988. participant._status = status;
  989. conference.eventEmitter.emit(JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  990. });
  991. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  992. if(conference.lastDominantSpeaker !== id && conference.room) {
  993. conference.lastDominantSpeaker = id;
  994. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  995. }
  996. if (conference.statistics && conference.myUserId() === id) {
  997. // We are the new dominant speaker.
  998. conference.statistics.sendDominantSpeakerEvent();
  999. }
  1000. });
  1001. conference.rtc.addListener(RTCEvents.DATA_CHANNEL_OPEN, function () {
  1002. var now = window.performance.now();
  1003. logger.log("(TIME) data channel opened ", now);
  1004. conference.room.connectionTimes["data.channel.opened"] = now;
  1005. });
  1006. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  1007. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  1008. });
  1009. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  1010. function (lastNEndpoints, endpointsEnteringLastN) {
  1011. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  1012. lastNEndpoints, endpointsEnteringLastN);
  1013. });
  1014. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  1015. function (audioMuted, videoMuted) {
  1016. conference.startAudioMuted = audioMuted;
  1017. conference.startVideoMuted = videoMuted;
  1018. // mute existing local tracks because this is initial mute from
  1019. // Jicofo
  1020. conference.getLocalTracks().forEach(function (track) {
  1021. if (conference.startAudioMuted && track.isAudioTrack()) {
  1022. track.mute();
  1023. }
  1024. if (conference.startVideoMuted && track.isVideoTrack()) {
  1025. track.mute();
  1026. }
  1027. });
  1028. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  1029. });
  1030. conference.room.addPresenceListener("startmuted", function (data, from) {
  1031. var isModerator = false;
  1032. if (conference.myUserId() === from && conference.isModerator()) {
  1033. isModerator = true;
  1034. } else {
  1035. var participant = conference.getParticipantById(from);
  1036. if (participant && participant.isModerator()) {
  1037. isModerator = true;
  1038. }
  1039. }
  1040. if (!isModerator) {
  1041. return;
  1042. }
  1043. var startAudioMuted = data.attributes.audio === 'true';
  1044. var startVideoMuted = data.attributes.video === 'true';
  1045. var updated = false;
  1046. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  1047. conference.startMutedPolicy.audio = startAudioMuted;
  1048. updated = true;
  1049. }
  1050. if (startVideoMuted !== conference.startMutedPolicy.video) {
  1051. conference.startMutedPolicy.video = startVideoMuted;
  1052. updated = true;
  1053. }
  1054. if (updated) {
  1055. conference.eventEmitter.emit(
  1056. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  1057. conference.startMutedPolicy
  1058. );
  1059. }
  1060. });
  1061. conference.rtc.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  1062. conference.room.updateDeviceAvailability(devices);
  1063. });
  1064. conference.room.addPresenceListener("devices", function (data, from) {
  1065. var isAudioAvailable = false;
  1066. var isVideoAvailable = false;
  1067. data.children.forEach(function (config) {
  1068. if (config.tagName === 'audio') {
  1069. isAudioAvailable = config.value === 'true';
  1070. }
  1071. if (config.tagName === 'video') {
  1072. isVideoAvailable = config.value === 'true';
  1073. }
  1074. });
  1075. var availableDevices;
  1076. if (conference.myUserId() === from) {
  1077. availableDevices = conference.availableDevices;
  1078. } else {
  1079. var participant = conference.getParticipantById(from);
  1080. if (!participant) {
  1081. return;
  1082. }
  1083. availableDevices = participant._availableDevices;
  1084. }
  1085. var updated = false;
  1086. if (availableDevices.audio !== isAudioAvailable) {
  1087. updated = true;
  1088. availableDevices.audio = isAudioAvailable;
  1089. }
  1090. if (availableDevices.video !== isVideoAvailable) {
  1091. updated = true;
  1092. availableDevices.video = isVideoAvailable;
  1093. }
  1094. if (updated) {
  1095. conference.eventEmitter.emit(
  1096. JitsiConferenceEvents.AVAILABLE_DEVICES_CHANGED,
  1097. from, availableDevices);
  1098. }
  1099. });
  1100. if(conference.statistics) {
  1101. //FIXME: Maybe remove event should not be associated with the conference.
  1102. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  1103. var userId = null;
  1104. var resource = conference.rtc.getResourceBySSRC(ssrc);
  1105. if (!resource)
  1106. return;
  1107. conference.rtc.setAudioLevel(resource, level);
  1108. });
  1109. conference.statistics.addConnectionStatsListener(function (stats) {
  1110. var ssrc2resolution = stats.resolution;
  1111. var id2resolution = {};
  1112. // preprocess resolutions: group by user id, skip incorrect
  1113. // resolutions etc.
  1114. Object.keys(ssrc2resolution).forEach(function (ssrc) {
  1115. var resolution = ssrc2resolution[ssrc];
  1116. if (!resolution.width || !resolution.height ||
  1117. resolution.width == -1 || resolution.height == -1) {
  1118. return;
  1119. }
  1120. var id = conference.rtc.getResourceBySSRC(ssrc);
  1121. if (!id) {
  1122. return;
  1123. }
  1124. // ssrc to resolution map for user id
  1125. var idResolutions = id2resolution[id] || {};
  1126. idResolutions[ssrc] = resolution;
  1127. id2resolution[id] = idResolutions;
  1128. });
  1129. stats.resolution = id2resolution;
  1130. conference.eventEmitter.emit(
  1131. JitsiConferenceEvents.CONNECTION_STATS, stats);
  1132. });
  1133. conference.room.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  1134. function () {
  1135. conference.statistics.dispose();
  1136. });
  1137. conference.room.addListener(XMPPEvents.PEERCONNECTION_READY,
  1138. function (session) {
  1139. conference.statistics.startCallStats(
  1140. session, conference.settings);
  1141. });
  1142. conference.room.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
  1143. function (pc) {
  1144. conference.statistics.sendIceConnectionFailedEvent(pc);
  1145. conference.room.eventEmitter.emit(
  1146. XMPPEvents.CONFERENCE_SETUP_FAILED);
  1147. });
  1148. conference.rtc.addListener(RTCEvents.TRACK_ATTACHED,
  1149. function(track, container) {
  1150. var ssrc = track.getSSRC();
  1151. if (!container.id || !ssrc) {
  1152. return;
  1153. }
  1154. conference.statistics.associateStreamWithVideoTag(
  1155. ssrc, track.isLocal(), track.getUsageLabel(), container.id);
  1156. });
  1157. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  1158. function (track) {
  1159. if(!track.isLocal())
  1160. return;
  1161. var type = (track.getType() === "audio")? "audio" : "video";
  1162. conference.statistics.sendMuteEvent(track.isMuted(), type);
  1163. });
  1164. conference.room.addListener(XMPPEvents.CREATE_OFFER_FAILED, function (e, pc) {
  1165. conference.statistics.sendCreateOfferFailed(e, pc);
  1166. });
  1167. conference.room.addListener(XMPPEvents.CREATE_ANSWER_FAILED, function (e, pc) {
  1168. conference.statistics.sendCreateAnswerFailed(e, pc);
  1169. });
  1170. conference.room.addListener(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  1171. function (e, pc) {
  1172. conference.statistics.sendSetLocalDescFailed(e, pc);
  1173. }
  1174. );
  1175. conference.room.addListener(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  1176. function (e, pc) {
  1177. conference.statistics.sendSetRemoteDescFailed(e, pc);
  1178. }
  1179. );
  1180. conference.room.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  1181. function (e, pc) {
  1182. conference.statistics.sendAddIceCandidateFailed(e, pc);
  1183. }
  1184. );
  1185. }
  1186. }
  1187. module.exports = JitsiConference;