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

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