Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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