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 41KB

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