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

JitsiConference.js 46KB

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