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

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