You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JitsiConference.js 40KB

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