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

JitsiConference.js 49KB

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