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

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