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

JitsiConference.js 40KB

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