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

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