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

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