You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JitsiConference.js 45KB

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