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

JitsiConference.js 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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(confID);
  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_MUTE_CHANGED,
  532. function () {
  533. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  534. }
  535. );
  536. track.addEventListener(
  537. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  538. function (audioLevel) {
  539. emitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  540. }
  541. );
  542. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  543. };
  544. JitsiConference.prototype.updateDTMFSupport = function () {
  545. var somebodySupportsDTMF = false;
  546. var participants = this.getParticipants();
  547. // check if at least 1 participant supports DTMF
  548. for (var i = 0; i < participants.length; i += 1) {
  549. if (participants[i].supportsDTMF()) {
  550. somebodySupportsDTMF = true;
  551. break;
  552. }
  553. }
  554. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  555. this.somebodySupportsDTMF = somebodySupportsDTMF;
  556. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  557. }
  558. };
  559. /**
  560. * Allows to check if there is at least one user in the conference
  561. * that supports DTMF.
  562. * @returns {boolean} true if somebody supports DTMF, false otherwise
  563. */
  564. JitsiConference.prototype.isDTMFSupported = function () {
  565. return this.somebodySupportsDTMF;
  566. };
  567. /**
  568. * Returns the local user's ID
  569. * @return {string} local user's ID
  570. */
  571. JitsiConference.prototype.myUserId = function () {
  572. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  573. };
  574. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  575. if (!this.dtmfManager) {
  576. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  577. if (!connection) {
  578. logger.warn("cannot sendTones: no conneciton");
  579. return;
  580. }
  581. var tracks = this.getLocalTracks().filter(function (track) {
  582. return track.isAudioTrack();
  583. });
  584. if (!tracks.length) {
  585. logger.warn("cannot sendTones: no local audio stream");
  586. return;
  587. }
  588. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  589. }
  590. this.dtmfManager.sendTones(tones, duration, pause);
  591. };
  592. /**
  593. * Returns true if the recording is supproted and false if not.
  594. */
  595. JitsiConference.prototype.isRecordingSupported = function () {
  596. if(this.room)
  597. return this.room.isRecordingSupported();
  598. return false;
  599. };
  600. /**
  601. * Returns null if the recording is not supported, "on" if the recording started
  602. * and "off" if the recording is not started.
  603. */
  604. JitsiConference.prototype.getRecordingState = function () {
  605. if(this.room)
  606. return this.room.getRecordingState();
  607. return "off";
  608. }
  609. /**
  610. * Returns the url of the recorded video.
  611. */
  612. JitsiConference.prototype.getRecordingURL = function () {
  613. if(this.room)
  614. return this.room.getRecordingURL();
  615. return null;
  616. }
  617. /**
  618. * Starts/stops the recording
  619. */
  620. JitsiConference.prototype.toggleRecording = function (options) {
  621. if(this.room)
  622. return this.room.toggleRecording(options, function (status, error) {
  623. this.eventEmitter.emit(
  624. JitsiConferenceEvents.RECORDING_STATE_CHANGED, status, error);
  625. }.bind(this));
  626. this.eventEmitter.emit(
  627. JitsiConferenceEvents.RECORDING_STATE_CHANGED, "error",
  628. new Error("The conference is not created yet!"));
  629. }
  630. /**
  631. * Returns true if the SIP calls are supported and false otherwise
  632. */
  633. JitsiConference.prototype.isSIPCallingSupported = function () {
  634. if(this.room)
  635. return this.room.isSIPCallingSupported();
  636. return false;
  637. }
  638. /**
  639. * Dials a number.
  640. * @param number the number
  641. */
  642. JitsiConference.prototype.dial = function (number) {
  643. if(this.room)
  644. return this.room.dial(number);
  645. return new Promise(function(resolve, reject){
  646. reject(new Error("The conference is not created yet!"))});
  647. }
  648. /**
  649. * Hangup an existing call
  650. */
  651. JitsiConference.prototype.hangup = function () {
  652. if(this.room)
  653. return this.room.hangup();
  654. return new Promise(function(resolve, reject){
  655. reject(new Error("The conference is not created yet!"))});
  656. }
  657. /**
  658. * Returns the phone number for joining the conference.
  659. */
  660. JitsiConference.prototype.getPhoneNumber = function () {
  661. if(this.room)
  662. return this.room.getPhoneNumber();
  663. return null;
  664. }
  665. /**
  666. * Returns the pin for joining the conference with phone.
  667. */
  668. JitsiConference.prototype.getPhonePin = function () {
  669. if(this.room)
  670. return this.room.getPhonePin();
  671. return null;
  672. }
  673. /**
  674. * Returns the connection state for the current room. Its ice connection state
  675. * for its session.
  676. */
  677. JitsiConference.prototype.getConnectionState = function () {
  678. if(this.room)
  679. return this.room.getConnectionState();
  680. return null;
  681. }
  682. /**
  683. * Make all new participants mute their audio/video on join.
  684. * @param policy {Object} object with 2 boolean properties for video and audio:
  685. * @param {boolean} audio if audio should be muted.
  686. * @param {boolean} video if video should be muted.
  687. */
  688. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  689. if (!this.isModerator()) {
  690. return;
  691. }
  692. this.startMutedPolicy = policy;
  693. this.room.removeFromPresence("startmuted");
  694. this.room.addToPresence("startmuted", {
  695. attributes: {
  696. audio: policy.audio,
  697. video: policy.video,
  698. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  699. }
  700. });
  701. this.room.sendPresence();
  702. };
  703. /**
  704. * Returns current start muted policy
  705. * @returns {Object} with 2 proprties - audio and video.
  706. */
  707. JitsiConference.prototype.getStartMutedPolicy = function () {
  708. return this.startMutedPolicy;
  709. };
  710. /**
  711. * Check if audio is muted on join.
  712. */
  713. JitsiConference.prototype.isStartAudioMuted = function () {
  714. return this.startAudioMuted;
  715. };
  716. /**
  717. * Check if video is muted on join.
  718. */
  719. JitsiConference.prototype.isStartVideoMuted = function () {
  720. return this.startVideoMuted;
  721. };
  722. /**
  723. * Get object with internal logs.
  724. */
  725. JitsiConference.prototype.getLogs = function () {
  726. var data = this.xmpp.getJingleLog();
  727. var metadata = {};
  728. metadata.time = new Date();
  729. metadata.url = window.location.href;
  730. metadata.ua = navigator.userAgent;
  731. var log = this.xmpp.getXmppLog();
  732. if (log) {
  733. metadata.xmpp = log;
  734. }
  735. data.metadata = metadata;
  736. return data;
  737. };
  738. /**
  739. * Sends the given feedback through CallStats if enabled.
  740. *
  741. * @param overallFeedback an integer between 1 and 5 indicating the
  742. * user feedback
  743. * @param detailedFeedback detailed feedback from the user. Not yet used
  744. */
  745. JitsiConference.prototype.sendFeedback =
  746. function(overallFeedback, detailedFeedback){
  747. this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  748. }
  749. /**
  750. * Returns true if the callstats integration is enabled, otherwise returns
  751. * false.
  752. *
  753. * @returns true if the callstats integration is enabled, otherwise returns
  754. * false.
  755. */
  756. JitsiConference.prototype.isCallstatsEnabled = function () {
  757. return this.statistics.isCallstatsEnabled();
  758. }
  759. /**
  760. * Setups the listeners needed for the conference.
  761. * @param conference the conference
  762. */
  763. function setupListeners(conference) {
  764. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  765. conference.rtc.onIncommingCall(event);
  766. conference.statistics.startRemoteStats(event.peerconnection);
  767. });
  768. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  769. function (data, sid, thessrc) {
  770. var track = conference.rtc.createRemoteStream(data, sid, thessrc);
  771. if (track) {
  772. conference.onTrackAdded(track);
  773. }
  774. }
  775. );
  776. conference.room.addListener(XMPPEvents.REMOTE_STREAM_REMOVED,
  777. function (streamId) {
  778. var participants = conference.getParticipants();
  779. for(var j = 0; j < participants.length; j++) {
  780. var participant = participants[j];
  781. var tracks = participant.getTracks();
  782. for(var i = 0; i < tracks.length; i++) {
  783. if(tracks[i] && tracks[i].stream &&
  784. RTC.getStreamID(tracks[i].stream) == streamId){
  785. var track = participant._tracks.splice(i, 1);
  786. conference.eventEmitter.emit(
  787. JitsiConferenceEvents.TRACK_REMOVED, track);
  788. return;
  789. }
  790. }
  791. }
  792. }
  793. );
  794. conference.rtc.addListener(RTCEvents.FAKE_VIDEO_TRACK_CREATED,
  795. function (track) {
  796. conference.onTrackAdded(track);
  797. }
  798. );
  799. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  800. function (value) {
  801. conference.rtc.setAudioMute(value);
  802. conference.isMutedByFocus = true;
  803. }
  804. );
  805. conference.room.addListener(XMPPEvents.SUBJECT_CHANGED, function (subject) {
  806. conference.eventEmitter.emit(JitsiConferenceEvents.SUBJECT_CHANGED, subject);
  807. });
  808. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  809. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  810. });
  811. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  812. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  813. });
  814. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  815. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  816. });
  817. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  818. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  819. });
  820. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  821. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  822. });
  823. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  824. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  825. });
  826. conference.room.addListener(XMPPEvents.RESERVATION_ERROR, function (code, msg) {
  827. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.RESERVATION_ERROR, code, msg);
  828. });
  829. conference.room.addListener(XMPPEvents.GRACEFUL_SHUTDOWN, function () {
  830. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  831. });
  832. conference.room.addListener(XMPPEvents.JINGLE_FATAL_ERROR, function () {
  833. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.JINGLE_FATAL_ERROR);
  834. });
  835. conference.room.addListener(XMPPEvents.MUC_DESTROYED, function (reason) {
  836. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONFERENCE_DESTROYED, reason);
  837. });
  838. conference.room.addListener(XMPPEvents.CHAT_ERROR_RECEIVED, function (err, msg) {
  839. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_ERROR, JitsiConferenceErrors.CHAT_ERROR, err, msg);
  840. });
  841. conference.room.addListener(XMPPEvents.FOCUS_DISCONNECTED, function (focus, retrySec) {
  842. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_DISCONNECTED, focus, retrySec);
  843. });
  844. conference.room.addListener(XMPPEvents.FOCUS_LEFT, function () {
  845. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_LEFT);
  846. });
  847. // FIXME
  848. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  849. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  850. // });
  851. conference.room.addListener(XMPPEvents.KICKED, function () {
  852. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  853. });
  854. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  855. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  856. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  857. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  858. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  859. });
  860. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  861. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  862. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  863. });
  864. conference.room.addListener(XMPPEvents.RECORDING_STATE_CHANGED,
  865. function () {
  866. conference.eventEmitter.emit(
  867. JitsiConferenceEvents.RECORDING_STATE_CHANGED);
  868. });
  869. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  870. conference.eventEmitter.emit(
  871. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  872. });
  873. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  874. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  875. });
  876. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  877. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.SETUP_FAILED);
  878. });
  879. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  880. conference.authEnabled = authEnabled;
  881. conference.authIdentity = authIdentity;
  882. });
  883. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  884. var id = Strophe.getResourceFromJid(jid);
  885. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  886. });
  887. conference.room.addListener(XMPPEvents.PRESENCE_STATUS, function (jid, status) {
  888. var id = Strophe.getResourceFromJid(jid);
  889. var participant = conference.getParticipantById(id);
  890. if (!participant || participant._status === status) {
  891. return;
  892. }
  893. participant._status = status;
  894. conference.eventEmitter.emit(JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  895. });
  896. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  897. if(conference.lastDominantSpeaker !== id && conference.room) {
  898. conference.lastDominantSpeaker = id;
  899. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  900. }
  901. });
  902. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  903. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  904. });
  905. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  906. function (lastNEndpoints, endpointsEnteringLastN) {
  907. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  908. lastNEndpoints, endpointsEnteringLastN);
  909. });
  910. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  911. function (audioMuted, videoMuted) {
  912. conference.startAudioMuted = audioMuted;
  913. conference.startVideoMuted = videoMuted;
  914. // mute existing local tracks because this is initial mute from
  915. // Jicofo
  916. conference.getLocalTracks().forEach(function (track) {
  917. if (conference.startAudioMuted && track.isAudioTrack()) {
  918. track.mute();
  919. }
  920. if (conference.startVideoMuted && track.isVideoTrack()) {
  921. track.mute();
  922. }
  923. });
  924. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  925. });
  926. conference.room.addPresenceListener("startmuted", function (data, from) {
  927. var isModerator = false;
  928. if (conference.myUserId() === from && conference.isModerator()) {
  929. isModerator = true;
  930. } else {
  931. var participant = conference.getParticipantById(from);
  932. if (participant && participant.isModerator()) {
  933. isModerator = true;
  934. }
  935. }
  936. if (!isModerator) {
  937. return;
  938. }
  939. var startAudioMuted = data.attributes.audio === 'true';
  940. var startVideoMuted = data.attributes.video === 'true';
  941. var updated = false;
  942. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  943. conference.startMutedPolicy.audio = startAudioMuted;
  944. updated = true;
  945. }
  946. if (startVideoMuted !== conference.startMutedPolicy.video) {
  947. conference.startMutedPolicy.video = startVideoMuted;
  948. updated = true;
  949. }
  950. if (updated) {
  951. conference.eventEmitter.emit(
  952. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  953. conference.startMutedPolicy
  954. );
  955. }
  956. });
  957. conference.rtc.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  958. conference.room.updateDeviceAvailability(devices);
  959. });
  960. conference.room.addPresenceListener("devices", function (data, from) {
  961. var isAudioAvailable = false;
  962. var isVideoAvailable = false;
  963. data.children.forEach(function (config) {
  964. if (config.tagName === 'audio') {
  965. isAudioAvailable = config.value === 'true';
  966. }
  967. if (config.tagName === 'video') {
  968. isVideoAvailable = config.value === 'true';
  969. }
  970. });
  971. var availableDevices;
  972. if (conference.myUserId() === from) {
  973. availableDevices = conference.availableDevices;
  974. } else {
  975. var participant = conference.getParticipantById(from);
  976. if (!participant) {
  977. return;
  978. }
  979. availableDevices = participant._availableDevices;
  980. }
  981. var updated = false;
  982. if (availableDevices.audio !== isAudioAvailable) {
  983. updated = true;
  984. availableDevices.audio = isAudioAvailable;
  985. }
  986. if (availableDevices.video !== isVideoAvailable) {
  987. updated = true;
  988. availableDevices.video = isVideoAvailable;
  989. }
  990. if (updated) {
  991. conference.eventEmitter.emit(
  992. JitsiConferenceEvents.AVAILABLE_DEVICES_CHANGED,
  993. from, availableDevices);
  994. }
  995. });
  996. if(conference.statistics) {
  997. //FIXME: Maybe remove event should not be associated with the conference.
  998. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  999. var userId = null;
  1000. var resource = conference.rtc.getResourceBySSRC(ssrc);
  1001. if (!resource)
  1002. return;
  1003. conference.rtc.setAudioLevel(resource, level);
  1004. });
  1005. conference.statistics.addConnectionStatsListener(function (stats) {
  1006. var ssrc2resolution = stats.resolution;
  1007. var id2resolution = {};
  1008. // preprocess resolutions: group by user id, skip incorrect
  1009. // resolutions etc.
  1010. Object.keys(ssrc2resolution).forEach(function (ssrc) {
  1011. var resolution = ssrc2resolution[ssrc];
  1012. if (!resolution.width || !resolution.height ||
  1013. resolution.width == -1 || resolution.height == -1) {
  1014. return;
  1015. }
  1016. var id = conference.rtc.getResourceBySSRC(ssrc);
  1017. if (!id) {
  1018. return;
  1019. }
  1020. // ssrc to resolution map for user id
  1021. var idResolutions = id2resolution[id] || {};
  1022. idResolutions[ssrc] = resolution;
  1023. id2resolution[id] = idResolutions;
  1024. });
  1025. stats.resolution = id2resolution;
  1026. conference.eventEmitter.emit(
  1027. JitsiConferenceEvents.CONNECTION_STATS, stats);
  1028. });
  1029. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  1030. function () {
  1031. conference.statistics.dispose();
  1032. });
  1033. conference.room.addListener(XMPPEvents.PEERCONNECTION_READY,
  1034. function (session) {
  1035. conference.statistics.startCallStats(
  1036. session, conference.settings);
  1037. });
  1038. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED,
  1039. function () {
  1040. conference.statistics.sendSetupFailedEvent();
  1041. });
  1042. conference.rtc.addListener(RTCEvents.TRACK_ATTACHED,
  1043. function(track, container) {
  1044. var ssrc = track.getSSRC();
  1045. if (!container.id || !ssrc) {
  1046. return;
  1047. }
  1048. conference.statistics.associateStreamWithVideoTag(
  1049. ssrc, track.isLocal(), track.getUsageLabel(), container.id);
  1050. });
  1051. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  1052. function (track) {
  1053. if(!track.isLocal())
  1054. return;
  1055. var type = (track.getType() === "audio")? "audio" : "video";
  1056. conference.statistics.sendMuteEvent(track.isMuted(), type);
  1057. });
  1058. conference.room.addListener(XMPPEvents.CREATE_OFFER_FAILED, function (e, pc) {
  1059. conference.statistics.sendCreateOfferFailed(e, pc);
  1060. });
  1061. conference.room.addListener(XMPPEvents.CREATE_ANSWER_FAILED, function (e, pc) {
  1062. conference.statistics.sendCreateAnswerFailed(e, pc);
  1063. });
  1064. conference.room.addListener(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  1065. function (e, pc) {
  1066. conference.statistics.sendSetLocalDescFailed(e, pc);
  1067. }
  1068. );
  1069. conference.room.addListener(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  1070. function (e, pc) {
  1071. conference.statistics.sendSetRemoteDescFailed(e, pc);
  1072. }
  1073. );
  1074. conference.room.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  1075. function (e, pc) {
  1076. conference.statistics.sendAddIceCandidateFailed(e, pc);
  1077. }
  1078. );
  1079. }
  1080. }
  1081. module.exports = JitsiConference;