Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

JitsiConference.js 46KB

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