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

JitsiConference.js 39KB

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