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

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