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

JitsiConference.js 49KB

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