Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JitsiConference.js 47KB

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