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

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