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

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