Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JitsiConference.js 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. /* global Strophe, $, Promise */
  2. /* jshint -W101 */
  3. var logger = require("jitsi-meet-logger").getLogger(__filename);
  4. var RTC = require("./modules/RTC/RTC");
  5. var XMPPEvents = require("./service/xmpp/XMPPEvents");
  6. var AuthenticationEvents = require("./service/authentication/AuthenticationEvents");
  7. var RTCEvents = require("./service/RTC/RTCEvents");
  8. var EventEmitter = require("events");
  9. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  10. var JitsiConferenceErrors = require("./JitsiConferenceErrors");
  11. var JitsiParticipant = require("./JitsiParticipant");
  12. var Statistics = require("./modules/statistics/statistics");
  13. var JitsiDTMFManager = require('./modules/DTMF/JitsiDTMFManager');
  14. var JitsiTrackEvents = require("./JitsiTrackEvents");
  15. var JitsiTrackErrors = require("./JitsiTrackErrors");
  16. var JitsiTrackError = require("./JitsiTrackError");
  17. var Settings = require("./modules/settings/Settings");
  18. var ComponentsVersions = require("./modules/version/ComponentsVersions");
  19. var GlobalOnErrorHandler = require("./modules/util/GlobalOnErrorHandler");
  20. /**
  21. * Creates a JitsiConference object with the given name and properties.
  22. * Note: this constructor is not a part of the public API (objects should be
  23. * created using JitsiConnection.createConference).
  24. * @param options.config properties / settings related to the conference that will be created.
  25. * @param options.name the name of the conference
  26. * @param options.connection the JitsiConnection object for this JitsiConference.
  27. * @constructor
  28. */
  29. function JitsiConference(options) {
  30. if(!options.name || options.name.toLowerCase() !== options.name) {
  31. var errmsg
  32. = "Invalid conference name (no conference name passed or it "
  33. + "contains invalid characters like capital letters)!";
  34. logger.error(errmsg);
  35. throw new Error(errmsg);
  36. }
  37. this.options = options;
  38. this.connection = this.options.connection;
  39. this.xmpp = this.connection.xmpp;
  40. this.eventEmitter = new EventEmitter();
  41. var confID = this.options.name + '@' + this.xmpp.options.hosts.muc;
  42. this.settings = new Settings();
  43. this.room = this.xmpp.createRoom(this.options.name, this.options.config,
  44. this.settings);
  45. this.componentsVersions = new ComponentsVersions(this.room);
  46. this.room.updateDeviceAvailability(RTC.getDeviceAvailability());
  47. this.rtc = new RTC(this.room, options);
  48. this.statistics = new Statistics(this.xmpp, {
  49. audioLevelsInterval: this.options.config.audioLevelsInterval,
  50. callStatsID: this.options.config.callStatsID,
  51. callStatsSecret: this.options.config.callStatsSecret,
  52. disableThirdPartyRequests:
  53. this.options.config.disableThirdPartyRequests,
  54. roomName: this.options.name
  55. });
  56. setupListeners(this);
  57. var JitsiMeetJS = this.connection.JitsiMeetJS;
  58. JitsiMeetJS._gumFailedHandler.push(function(error) {
  59. this.statistics.sendGetUserMediaFailed(error);
  60. }.bind(this));
  61. JitsiMeetJS._globalOnErrorHandler.push(function(error) {
  62. if (error instanceof JitsiTrackError && error.gum) {
  63. this.statistics.sendGetUserMediaFailed(error);
  64. } else {
  65. this.statistics.sendUnhandledError(error);
  66. }
  67. }.bind(this));
  68. this.participants = {};
  69. this.lastDominantSpeaker = null;
  70. this.dtmfManager = null;
  71. this.somebodySupportsDTMF = false;
  72. this.authEnabled = false;
  73. this.authIdentity;
  74. this.startAudioMuted = false;
  75. this.startVideoMuted = false;
  76. this.startMutedPolicy = {audio: false, video: false};
  77. this.availableDevices = {
  78. audio: undefined,
  79. video: undefined
  80. };
  81. this.isMutedByFocus = false;
  82. }
  83. /**
  84. * Joins the conference.
  85. * @param password {string} the password
  86. */
  87. JitsiConference.prototype.join = function (password) {
  88. if(this.room)
  89. this.room.join(password);
  90. };
  91. /**
  92. * Check if joined to the conference.
  93. */
  94. JitsiConference.prototype.isJoined = function () {
  95. return this.room && this.room.joined;
  96. };
  97. /**
  98. * Leaves the conference and calls onMemberLeft for every participant.
  99. */
  100. JitsiConference.prototype._leaveRoomAndRemoveParticipants = function () {
  101. // leave the conference
  102. if (this.room) {
  103. this.room.leave();
  104. }
  105. this.room = null;
  106. // remove all participants
  107. this.getParticipants().forEach(function (participant) {
  108. this.onMemberLeft(participant.getJid());
  109. }.bind(this));
  110. }
  111. /**
  112. * Leaves the conference.
  113. * @returns {Promise}
  114. */
  115. JitsiConference.prototype.leave = function () {
  116. var conference = this;
  117. return Promise.all(
  118. conference.getLocalTracks().map(function (track) {
  119. return conference.removeTrack(track);
  120. })
  121. ).then(this._leaveRoomAndRemoveParticipants.bind(this))
  122. .catch(function (error) {
  123. logger.error(error);
  124. GlobalOnErrorHandler.callUnhandledRejectionHandler(
  125. {promise: this, reason: error});
  126. // We are proceeding with leaving the conference because room.leave may
  127. // succeed.
  128. this._leaveRoomAndRemoveParticipants();
  129. return Promise.resolve();
  130. }.bind(this));
  131. };
  132. /**
  133. * Returns name of this conference.
  134. */
  135. JitsiConference.prototype.getName = function () {
  136. return this.options.name;
  137. };
  138. /**
  139. * Check if authentication is enabled for this conference.
  140. */
  141. JitsiConference.prototype.isAuthEnabled = function () {
  142. return this.authEnabled;
  143. };
  144. /**
  145. * Check if user is logged in.
  146. */
  147. JitsiConference.prototype.isLoggedIn = function () {
  148. return !!this.authIdentity;
  149. };
  150. /**
  151. * Get authorized login.
  152. */
  153. JitsiConference.prototype.getAuthLogin = function () {
  154. return this.authIdentity;
  155. };
  156. /**
  157. * Check if external authentication is enabled for this conference.
  158. */
  159. JitsiConference.prototype.isExternalAuthEnabled = function () {
  160. return this.room && this.room.moderator.isExternalAuthEnabled();
  161. };
  162. /**
  163. * Get url for external authentication.
  164. * @param {boolean} [urlForPopup] if true then return url for login popup,
  165. * else url of login page.
  166. * @returns {Promise}
  167. */
  168. JitsiConference.prototype.getExternalAuthUrl = function (urlForPopup) {
  169. return new Promise(function (resolve, reject) {
  170. if (!this.isExternalAuthEnabled()) {
  171. reject();
  172. return;
  173. }
  174. if (urlForPopup) {
  175. this.room.moderator.getPopupLoginUrl(resolve, reject);
  176. } else {
  177. this.room.moderator.getLoginUrl(resolve, reject);
  178. }
  179. }.bind(this));
  180. };
  181. /**
  182. * Returns the local tracks.
  183. */
  184. JitsiConference.prototype.getLocalTracks = function () {
  185. if (this.rtc) {
  186. return this.rtc.localTracks.slice();
  187. } else {
  188. return [];
  189. }
  190. };
  191. /**
  192. * Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
  193. * in JitsiConferenceEvents.
  194. * @param eventId the event ID.
  195. * @param handler handler for the event.
  196. *
  197. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  198. */
  199. JitsiConference.prototype.on = function (eventId, handler) {
  200. if(this.eventEmitter)
  201. this.eventEmitter.on(eventId, handler);
  202. };
  203. /**
  204. * Removes event listener
  205. * @param eventId the event ID.
  206. * @param [handler] optional, the specific handler to unbind
  207. *
  208. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  209. */
  210. JitsiConference.prototype.off = function (eventId, handler) {
  211. if(this.eventEmitter)
  212. this.eventEmitter.removeListener(eventId, handler);
  213. };
  214. // Common aliases for event emitter
  215. JitsiConference.prototype.addEventListener = JitsiConference.prototype.on;
  216. JitsiConference.prototype.removeEventListener = JitsiConference.prototype.off;
  217. /**
  218. * Receives notifications from other participants about commands / custom events
  219. * (sent by sendCommand or sendCommandOnce methods).
  220. * @param command {String} the name of the command
  221. * @param handler {Function} handler for the command
  222. */
  223. JitsiConference.prototype.addCommandListener = function (command, handler) {
  224. if(this.room)
  225. this.room.addPresenceListener(command, handler);
  226. };
  227. /**
  228. * Removes command listener
  229. * @param command {String} the name of the command
  230. */
  231. JitsiConference.prototype.removeCommandListener = function (command) {
  232. if(this.room)
  233. this.room.removePresenceListener(command);
  234. };
  235. /**
  236. * Sends text message to the other participants in the conference
  237. * @param message the text message.
  238. */
  239. JitsiConference.prototype.sendTextMessage = function (message) {
  240. if(this.room)
  241. this.room.sendMessage(message);
  242. };
  243. /**
  244. * Send presence command.
  245. * @param name {String} the name of the command.
  246. * @param values {Object} with keys and values that will be sent.
  247. **/
  248. JitsiConference.prototype.sendCommand = function (name, values) {
  249. if(this.room) {
  250. this.room.addToPresence(name, values);
  251. this.room.sendPresence();
  252. }
  253. };
  254. /**
  255. * Send presence command one time.
  256. * @param name {String} the name of the command.
  257. * @param values {Object} with keys and values that will be sent.
  258. **/
  259. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  260. this.sendCommand(name, values);
  261. this.removeCommand(name);
  262. };
  263. /**
  264. * Removes presence command.
  265. * @param name {String} the name of the command.
  266. **/
  267. JitsiConference.prototype.removeCommand = function (name) {
  268. if(this.room)
  269. this.room.removeFromPresence(name);
  270. };
  271. /**
  272. * Sets the display name for this conference.
  273. * @param name the display name to set
  274. */
  275. JitsiConference.prototype.setDisplayName = function(name) {
  276. if(this.room){
  277. // remove previously set nickname
  278. this.room.removeFromPresence("nick");
  279. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  280. this.room.sendPresence();
  281. }
  282. };
  283. /**
  284. * Set new subject for this conference. (available only for moderator)
  285. * @param {string} subject new subject
  286. */
  287. JitsiConference.prototype.setSubject = function (subject) {
  288. if (this.room && this.isModerator()) {
  289. this.room.setSubject(subject);
  290. }
  291. };
  292. /**
  293. * Adds JitsiLocalTrack object to the conference.
  294. * @param track the JitsiLocalTrack object.
  295. * @returns {Promise<JitsiLocalTrack>}
  296. * @throws will throw and error if track is video track
  297. * and there is already another video track in the conference.
  298. */
  299. JitsiConference.prototype.addTrack = function (track) {
  300. if(track.disposed)
  301. {
  302. throw new JitsiTrackError(JitsiTrackErrors.TRACK_IS_DISPOSED);
  303. }
  304. if (track.isVideoTrack() && this.rtc.getLocalVideoTrack()) {
  305. throw new Error("cannot add second video track to the conference");
  306. }
  307. track.ssrcHandler = function (conference, ssrcMap) {
  308. if(ssrcMap[this.getMSID()]){
  309. this._setSSRC(ssrcMap[this.getMSID()]);
  310. conference.room.removeListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  311. this.ssrcHandler);
  312. }
  313. }.bind(track, this);
  314. this.room.addListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  315. track.ssrcHandler);
  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. * Sends the given feedback through CallStats if enabled.
  818. *
  819. * @param overallFeedback an integer between 1 and 5 indicating the
  820. * user feedback
  821. * @param detailedFeedback detailed feedback from the user. Not yet used
  822. */
  823. JitsiConference.prototype.sendFeedback =
  824. function(overallFeedback, detailedFeedback){
  825. this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  826. }
  827. /**
  828. * Returns true if the callstats integration is enabled, otherwise returns
  829. * false.
  830. *
  831. * @returns true if the callstats integration is enabled, otherwise returns
  832. * false.
  833. */
  834. JitsiConference.prototype.isCallstatsEnabled = function () {
  835. return this.statistics.isCallstatsEnabled();
  836. }
  837. /**
  838. * Setups the listeners needed for the conference.
  839. * @param conference the conference
  840. */
  841. function setupListeners(conference) {
  842. conference.xmpp.addListener(
  843. XMPPEvents.CALL_INCOMING, function (jingleSession, jingleOffer, now) {
  844. if (conference.room.isFocus(jingleSession.peerjid)) {
  845. // Accept incoming call
  846. conference.room.setJingleSession(jingleSession);
  847. conference.room.connectionTimes["session.initiate"] = now;
  848. try{
  849. jingleSession.initialize(false /* initiator */,
  850. conference.room);
  851. } catch (error) {
  852. GlobalOnErrorHandler.callErrorHandler(error);
  853. };
  854. conference.rtc.onIncommingCall(jingleSession);
  855. jingleSession.acceptOffer(jingleOffer, null,
  856. function (error) {
  857. GlobalOnErrorHandler.callErrorHandler(error);
  858. logger.error(
  859. "Failed to accept incoming Jingle session", error);
  860. }
  861. );
  862. // Start callstats as soon as peerconnection is initialized,
  863. // do not wait for XMPPEvents.PEERCONNECTION_READY, as it may never
  864. // happen in case if user doesn't have or denied permission to
  865. // both camera and microphone.
  866. conference.statistics.startCallStats(jingleSession, conference.settings);
  867. conference.statistics.startRemoteStats(jingleSession.peerconnection);
  868. } else {
  869. // Error cause this should never happen unless something is wrong!
  870. var errmsg
  871. = "Rejecting session-initiate from non-focus user: "
  872. + jingleSession.peerjid;
  873. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  874. logger.error(errmsg);
  875. }
  876. });
  877. conference.room.addListener(XMPPEvents.ICE_RESTARTING, function () {
  878. // All data channels have to be closed, before ICE restart
  879. // otherwise Chrome will not trigger "opened" event for the channel
  880. // established with the new bridge
  881. conference.rtc.closeAllDataChannels();
  882. });
  883. conference.room.addListener(XMPPEvents.REMOTE_TRACK_ADDED,
  884. function (data) {
  885. var track = conference.rtc.createRemoteTrack(data);
  886. if (track) {
  887. conference.onTrackAdded(track);
  888. }
  889. }
  890. );
  891. conference.room.addListener(XMPPEvents.REMOTE_TRACK_REMOVED,
  892. function (streamId, trackId) {
  893. conference.getParticipants().forEach(function(participant) {
  894. var tracks = participant.getTracks();
  895. for(var i = 0; i < tracks.length; i++) {
  896. if(tracks[i]
  897. && tracks[i].getStreamId() == streamId
  898. && tracks[i].getTrackId() == trackId) {
  899. var track = participant._tracks.splice(i, 1)[0];
  900. conference.eventEmitter.emit(
  901. JitsiConferenceEvents.TRACK_REMOVED, track);
  902. return;
  903. }
  904. }
  905. });
  906. }
  907. );
  908. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  909. function (value) {
  910. // set isMutedByFocus when setAudioMute Promise ends
  911. conference.rtc.setAudioMute(value).then(
  912. function() {
  913. conference.isMutedByFocus = true;
  914. },
  915. function() {
  916. logger.warn(
  917. "Error while audio muting due to focus request");
  918. });
  919. }
  920. );
  921. conference.room.addListener(XMPPEvents.SUBJECT_CHANGED, function (subject) {
  922. conference.eventEmitter.emit(JitsiConferenceEvents.SUBJECT_CHANGED,
  923. subject);
  924. });
  925. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  926. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  927. });
  928. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  929. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  930. JitsiConferenceErrors.CONNECTION_ERROR, pres);
  931. });
  932. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  933. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  934. JitsiConferenceErrors.CONNECTION_ERROR, pres);
  935. });
  936. conference.room.addListener(XMPPEvents.ROOM_MAX_USERS_ERROR,
  937. function (pres) {
  938. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  939. JitsiConferenceErrors.CONFERENCE_MAX_USERS, pres);
  940. });
  941. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  942. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  943. });
  944. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  945. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  946. });
  947. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  948. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  949. });
  950. conference.room.addListener(XMPPEvents.RESERVATION_ERROR, function (code, msg) {
  951. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.RESERVATION_ERROR, code, msg);
  952. });
  953. conference.room.addListener(XMPPEvents.GRACEFUL_SHUTDOWN, function () {
  954. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  955. });
  956. conference.room.addListener(XMPPEvents.JINGLE_FATAL_ERROR, function (session, error) {
  957. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.JINGLE_FATAL_ERROR, error);
  958. });
  959. conference.room.addListener(XMPPEvents.MUC_DESTROYED, function (reason) {
  960. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONFERENCE_DESTROYED, reason);
  961. });
  962. conference.room.addListener(XMPPEvents.CHAT_ERROR_RECEIVED, function (err, msg) {
  963. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_ERROR, JitsiConferenceErrors.CHAT_ERROR, err, msg);
  964. });
  965. conference.room.addListener(XMPPEvents.FOCUS_DISCONNECTED, function (focus, retrySec) {
  966. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_DISCONNECTED, focus, retrySec);
  967. });
  968. conference.room.addListener(XMPPEvents.FOCUS_LEFT, function () {
  969. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_LEFT);
  970. });
  971. // FIXME
  972. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  973. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  974. // });
  975. conference.room.addListener(XMPPEvents.KICKED, function () {
  976. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  977. });
  978. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  979. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  980. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  981. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  982. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  983. });
  984. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  985. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  986. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  987. });
  988. conference.room.addListener(XMPPEvents.RECORDER_STATE_CHANGED,
  989. function (state) {
  990. conference.eventEmitter.emit(
  991. JitsiConferenceEvents.RECORDER_STATE_CHANGED, state);
  992. });
  993. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  994. conference.eventEmitter.emit(
  995. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  996. });
  997. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  998. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  999. });
  1000. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED,
  1001. function (error) {
  1002. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1003. JitsiConferenceErrors.SETUP_FAILED, error);
  1004. });
  1005. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  1006. conference.authEnabled = authEnabled;
  1007. conference.authIdentity = authIdentity;
  1008. conference.eventEmitter.emit(JitsiConferenceEvents.AUTH_STATUS_CHANGED, authEnabled, authIdentity);
  1009. });
  1010. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  1011. var id = Strophe.getResourceFromJid(jid);
  1012. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  1013. });
  1014. conference.room.addListener(XMPPEvents.PRESENCE_STATUS, function (jid, status) {
  1015. var id = Strophe.getResourceFromJid(jid);
  1016. var participant = conference.getParticipantById(id);
  1017. if (!participant || participant._status === status) {
  1018. return;
  1019. }
  1020. participant._status = status;
  1021. conference.eventEmitter.emit(JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  1022. });
  1023. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  1024. if(conference.lastDominantSpeaker !== id && conference.room) {
  1025. conference.lastDominantSpeaker = id;
  1026. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  1027. }
  1028. if (conference.statistics && conference.myUserId() === id) {
  1029. // We are the new dominant speaker.
  1030. conference.statistics.sendDominantSpeakerEvent();
  1031. }
  1032. });
  1033. conference.rtc.addListener(RTCEvents.DATA_CHANNEL_OPEN, function () {
  1034. var now = window.performance.now();
  1035. logger.log("(TIME) data channel opened ", now);
  1036. conference.room.connectionTimes["data.channel.opened"] = now;
  1037. });
  1038. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  1039. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  1040. });
  1041. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  1042. function (lastNEndpoints, endpointsEnteringLastN) {
  1043. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  1044. lastNEndpoints, endpointsEnteringLastN);
  1045. });
  1046. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  1047. function (audioMuted, videoMuted) {
  1048. conference.startAudioMuted = audioMuted;
  1049. conference.startVideoMuted = videoMuted;
  1050. // mute existing local tracks because this is initial mute from
  1051. // Jicofo
  1052. conference.getLocalTracks().forEach(function (track) {
  1053. if (conference.startAudioMuted && track.isAudioTrack()) {
  1054. track.mute();
  1055. }
  1056. if (conference.startVideoMuted && track.isVideoTrack()) {
  1057. track.mute();
  1058. }
  1059. });
  1060. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  1061. });
  1062. conference.room.addPresenceListener("startmuted", function (data, from) {
  1063. var isModerator = false;
  1064. if (conference.myUserId() === from && conference.isModerator()) {
  1065. isModerator = true;
  1066. } else {
  1067. var participant = conference.getParticipantById(from);
  1068. if (participant && participant.isModerator()) {
  1069. isModerator = true;
  1070. }
  1071. }
  1072. if (!isModerator) {
  1073. return;
  1074. }
  1075. var startAudioMuted = data.attributes.audio === 'true';
  1076. var startVideoMuted = data.attributes.video === 'true';
  1077. var updated = false;
  1078. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  1079. conference.startMutedPolicy.audio = startAudioMuted;
  1080. updated = true;
  1081. }
  1082. if (startVideoMuted !== conference.startMutedPolicy.video) {
  1083. conference.startMutedPolicy.video = startVideoMuted;
  1084. updated = true;
  1085. }
  1086. if (updated) {
  1087. conference.eventEmitter.emit(
  1088. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  1089. conference.startMutedPolicy
  1090. );
  1091. }
  1092. });
  1093. conference.rtc.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  1094. conference.room.updateDeviceAvailability(devices);
  1095. });
  1096. conference.room.addPresenceListener("devices", function (data, from) {
  1097. var isAudioAvailable = false;
  1098. var isVideoAvailable = false;
  1099. data.children.forEach(function (config) {
  1100. if (config.tagName === 'audio') {
  1101. isAudioAvailable = config.value === 'true';
  1102. }
  1103. if (config.tagName === 'video') {
  1104. isVideoAvailable = config.value === 'true';
  1105. }
  1106. });
  1107. var availableDevices;
  1108. if (conference.myUserId() === from) {
  1109. availableDevices = conference.availableDevices;
  1110. } else {
  1111. var participant = conference.getParticipantById(from);
  1112. if (!participant) {
  1113. return;
  1114. }
  1115. availableDevices = participant._availableDevices;
  1116. }
  1117. var updated = false;
  1118. if (availableDevices.audio !== isAudioAvailable) {
  1119. updated = true;
  1120. availableDevices.audio = isAudioAvailable;
  1121. }
  1122. if (availableDevices.video !== isVideoAvailable) {
  1123. updated = true;
  1124. availableDevices.video = isVideoAvailable;
  1125. }
  1126. if (updated) {
  1127. conference.eventEmitter.emit(
  1128. JitsiConferenceEvents.AVAILABLE_DEVICES_CHANGED,
  1129. from, availableDevices);
  1130. }
  1131. });
  1132. if(conference.statistics) {
  1133. //FIXME: Maybe remove event should not be associated with the conference.
  1134. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  1135. var userId = null;
  1136. var resource = conference.rtc.getResourceBySSRC(ssrc);
  1137. if (!resource)
  1138. return;
  1139. conference.rtc.setAudioLevel(resource, level);
  1140. });
  1141. conference.statistics.addConnectionStatsListener(function (stats) {
  1142. var ssrc2resolution = stats.resolution;
  1143. var id2resolution = {};
  1144. // preprocess resolutions: group by user id, skip incorrect
  1145. // resolutions etc.
  1146. Object.keys(ssrc2resolution).forEach(function (ssrc) {
  1147. var resolution = ssrc2resolution[ssrc];
  1148. if (!resolution.width || !resolution.height ||
  1149. resolution.width == -1 || resolution.height == -1) {
  1150. return;
  1151. }
  1152. var id = conference.rtc.getResourceBySSRC(ssrc);
  1153. if (!id) {
  1154. return;
  1155. }
  1156. // ssrc to resolution map for user id
  1157. var idResolutions = id2resolution[id] || {};
  1158. idResolutions[ssrc] = resolution;
  1159. id2resolution[id] = idResolutions;
  1160. });
  1161. stats.resolution = id2resolution;
  1162. conference.eventEmitter.emit(
  1163. JitsiConferenceEvents.CONNECTION_STATS, stats);
  1164. });
  1165. conference.room.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  1166. function () {
  1167. conference.statistics.dispose();
  1168. });
  1169. conference.room.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
  1170. function (pc) {
  1171. conference.statistics.sendIceConnectionFailedEvent(pc);
  1172. conference.room.eventEmitter.emit(
  1173. XMPPEvents.CONFERENCE_SETUP_FAILED,
  1174. new Error("ICE fail"));
  1175. });
  1176. conference.rtc.addListener(RTCEvents.TRACK_ATTACHED,
  1177. function(track, container) {
  1178. var ssrc = track.getSSRC();
  1179. if (!container.id || !ssrc) {
  1180. return;
  1181. }
  1182. conference.statistics.associateStreamWithVideoTag(
  1183. ssrc, track.isLocal(), track.getUsageLabel(), container.id);
  1184. });
  1185. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  1186. function (track) {
  1187. if(!track.isLocal())
  1188. return;
  1189. var type = (track.getType() === "audio")? "audio" : "video";
  1190. conference.statistics.sendMuteEvent(track.isMuted(), type);
  1191. });
  1192. conference.room.addListener(XMPPEvents.CREATE_OFFER_FAILED, function (e, pc) {
  1193. conference.statistics.sendCreateOfferFailed(e, pc);
  1194. });
  1195. conference.room.addListener(XMPPEvents.CREATE_ANSWER_FAILED, function (e, pc) {
  1196. conference.statistics.sendCreateAnswerFailed(e, pc);
  1197. });
  1198. conference.room.addListener(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  1199. function (e, pc) {
  1200. conference.statistics.sendSetLocalDescFailed(e, pc);
  1201. }
  1202. );
  1203. conference.room.addListener(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  1204. function (e, pc) {
  1205. conference.statistics.sendSetRemoteDescFailed(e, pc);
  1206. }
  1207. );
  1208. conference.room.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  1209. function (e, pc) {
  1210. conference.statistics.sendAddIceCandidateFailed(e, pc);
  1211. }
  1212. );
  1213. }
  1214. }
  1215. module.exports = JitsiConference;