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

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