Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JitsiConference.js 47KB

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