Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

JitsiConference.js 46KB

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