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

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