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

JitsiConference.js 41KB

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