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

JitsiConference.js 46KB

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