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 37KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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. */
  253. JitsiConference.prototype.addTrack = function (track) {
  254. if (track.isVideoTrack()) {
  255. this.removeCommand("videoType");
  256. this.sendCommand("videoType", {
  257. value: track.videoType,
  258. attributes: {
  259. xmlns: 'http://jitsi.org/jitmeet/video'
  260. }
  261. });
  262. }
  263. this.room.addStream(track.getOriginalStream(), function () {
  264. this.rtc.addLocalStream(track);
  265. if (track.startMuted) {
  266. track.mute();
  267. }
  268. track.muteHandler = this._fireMuteChangeEvent.bind(this, track);
  269. track.stopHandler = this.removeTrack.bind(this, track);
  270. track.audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  271. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
  272. track.muteHandler);
  273. track.addEventListener(JitsiTrackEvents.TRACK_STOPPED,
  274. track.stopHandler);
  275. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  276. track.audioLevelHandler);
  277. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  278. }.bind(this));
  279. };
  280. /**
  281. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event.
  282. * @param audioLevel the audio level
  283. */
  284. JitsiConference.prototype._fireAudioLevelChangeEvent = function (audioLevel) {
  285. this.eventEmitter.emit(
  286. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  287. this.myUserId(), audioLevel);
  288. };
  289. /**
  290. * Fires TRACK_MUTE_CHANGED change conference event.
  291. * @param track the JitsiTrack object related to the event.
  292. */
  293. JitsiConference.prototype._fireMuteChangeEvent = function (track) {
  294. // check if track was muted by focus and now is unmuted by user
  295. if (this.isMutedByFocus && track.isAudioTrack() && !track.isMuted()) {
  296. this.isMutedByFocus = false;
  297. // unmute local user on server
  298. this.room.muteParticipant(this.room.myroomjid, false);
  299. }
  300. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  301. };
  302. /**
  303. * Removes JitsiLocalTrack object to the conference.
  304. * @param track the JitsiLocalTrack object.
  305. */
  306. JitsiConference.prototype.removeTrack = function (track) {
  307. if(!this.room){
  308. if(this.rtc)
  309. this.rtc.removeLocalStream(track);
  310. return;
  311. }
  312. this.room.removeStream(track.getOriginalStream(), function(){
  313. this.rtc.removeLocalStream(track);
  314. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, track.muteHandler);
  315. track.removeEventListener(JitsiTrackEvents.TRACK_STOPPED, track.stopHandler);
  316. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, track.audioLevelHandler);
  317. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  318. }.bind(this));
  319. };
  320. /**
  321. * Get role of the local user.
  322. * @returns {string} user role: 'moderator' or 'none'
  323. */
  324. JitsiConference.prototype.getRole = function () {
  325. return this.room.role;
  326. };
  327. /**
  328. * Check if local user is moderator.
  329. * @returns {boolean} true if local user is moderator, false otherwise.
  330. */
  331. JitsiConference.prototype.isModerator = function () {
  332. return this.room.isModerator();
  333. };
  334. /**
  335. * Set password for the room.
  336. * @param {string} password new password for the room.
  337. * @returns {Promise}
  338. */
  339. JitsiConference.prototype.lock = function (password) {
  340. if (!this.isModerator()) {
  341. return Promise.reject();
  342. }
  343. var conference = this;
  344. return new Promise(function (resolve, reject) {
  345. conference.room.lockRoom(password || "", function () {
  346. resolve();
  347. }, function (err) {
  348. reject(err);
  349. }, function () {
  350. reject(JitsiConferenceErrors.PASSWORD_NOT_SUPPORTED);
  351. });
  352. });
  353. };
  354. /**
  355. * Remove password from the room.
  356. * @returns {Promise}
  357. */
  358. JitsiConference.prototype.unlock = function () {
  359. return this.lock();
  360. };
  361. /**
  362. * Elects the participant with the given id to be the selected participant or the speaker.
  363. * @param id the identifier of the participant
  364. */
  365. JitsiConference.prototype.selectParticipant = function(participantId) {
  366. if (this.rtc) {
  367. this.rtc.selectedEndpoint(participantId);
  368. }
  369. };
  370. /**
  371. *
  372. * @param id the identifier of the participant
  373. */
  374. JitsiConference.prototype.pinParticipant = function(participantId) {
  375. if (this.rtc) {
  376. this.rtc.pinEndpoint(participantId);
  377. }
  378. };
  379. /**
  380. * Returns the list of participants for this conference.
  381. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  382. */
  383. JitsiConference.prototype.getParticipants = function() {
  384. return Object.keys(this.participants).map(function (key) {
  385. return this.participants[key];
  386. }, this);
  387. };
  388. /**
  389. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  390. * undefined if there isn't one).
  391. * @param id the id of the participant.
  392. */
  393. JitsiConference.prototype.getParticipantById = function(id) {
  394. return this.participants[id];
  395. };
  396. /**
  397. * Kick participant from this conference.
  398. * @param {string} id id of the participant to kick
  399. */
  400. JitsiConference.prototype.kickParticipant = function (id) {
  401. var participant = this.getParticipantById(id);
  402. if (!participant) {
  403. return;
  404. }
  405. this.room.kick(participant.getJid());
  406. };
  407. /**
  408. * Kick participant from this conference.
  409. * @param {string} id id of the participant to kick
  410. */
  411. JitsiConference.prototype.muteParticipant = function (id) {
  412. var participant = this.getParticipantById(id);
  413. if (!participant) {
  414. return;
  415. }
  416. this.room.muteParticipant(participant.getJid(), true);
  417. };
  418. JitsiConference.prototype.onMemberJoined = function (jid, nick, role) {
  419. var id = Strophe.getResourceFromJid(jid);
  420. if (id === 'focus' || this.myUserId() === id) {
  421. return;
  422. }
  423. var participant = new JitsiParticipant(jid, this, nick);
  424. participant._role = role;
  425. this.participants[id] = participant;
  426. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id, participant);
  427. this.xmpp.connection.disco.info(
  428. jid, "node", function(iq) {
  429. participant._supportsDTMF = $(iq).find(
  430. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  431. this.updateDTMFSupport();
  432. }.bind(this)
  433. );
  434. };
  435. JitsiConference.prototype.onMemberLeft = function (jid) {
  436. var id = Strophe.getResourceFromJid(jid);
  437. if (id === 'focus' || this.myUserId() === id) {
  438. return;
  439. }
  440. var participant = this.participants[id];
  441. delete this.participants[id];
  442. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  443. };
  444. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  445. var id = Strophe.getResourceFromJid(jid);
  446. var participant = this.getParticipantById(id);
  447. if (!participant) {
  448. return;
  449. }
  450. participant._role = role;
  451. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  452. };
  453. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  454. var id = Strophe.getResourceFromJid(jid);
  455. var participant = this.getParticipantById(id);
  456. if (!participant) {
  457. return;
  458. }
  459. participant._displayName = displayName;
  460. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  461. };
  462. JitsiConference.prototype.onTrackAdded = function (track) {
  463. var id = track.getParticipantId();
  464. var participant = this.getParticipantById(id);
  465. if (!participant) {
  466. return;
  467. }
  468. // add track to JitsiParticipant
  469. participant._tracks.push(track);
  470. var emitter = this.eventEmitter;
  471. track.addEventListener(
  472. JitsiTrackEvents.TRACK_STOPPED,
  473. function () {
  474. // remove track from JitsiParticipant
  475. var pos = participant._tracks.indexOf(track);
  476. if (pos > -1) {
  477. participant._tracks.splice(pos, 1);
  478. }
  479. emitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  480. }
  481. );
  482. track.addEventListener(
  483. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  484. function () {
  485. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  486. }
  487. );
  488. track.addEventListener(
  489. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  490. function (audioLevel) {
  491. emitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  492. }
  493. );
  494. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  495. };
  496. JitsiConference.prototype.updateDTMFSupport = function () {
  497. var somebodySupportsDTMF = false;
  498. var participants = this.getParticipants();
  499. // check if at least 1 participant supports DTMF
  500. for (var i = 0; i < participants.length; i += 1) {
  501. if (participants[i].supportsDTMF()) {
  502. somebodySupportsDTMF = true;
  503. break;
  504. }
  505. }
  506. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  507. this.somebodySupportsDTMF = somebodySupportsDTMF;
  508. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  509. }
  510. };
  511. /**
  512. * Allows to check if there is at least one user in the conference
  513. * that supports DTMF.
  514. * @returns {boolean} true if somebody supports DTMF, false otherwise
  515. */
  516. JitsiConference.prototype.isDTMFSupported = function () {
  517. return this.somebodySupportsDTMF;
  518. };
  519. /**
  520. * Returns the local user's ID
  521. * @return {string} local user's ID
  522. */
  523. JitsiConference.prototype.myUserId = function () {
  524. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  525. };
  526. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  527. if (!this.dtmfManager) {
  528. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  529. if (!connection) {
  530. logger.warn("cannot sendTones: no conneciton");
  531. return;
  532. }
  533. var tracks = this.getLocalTracks().filter(function (track) {
  534. return track.isAudioTrack();
  535. });
  536. if (!tracks.length) {
  537. logger.warn("cannot sendTones: no local audio stream");
  538. return;
  539. }
  540. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  541. }
  542. this.dtmfManager.sendTones(tones, duration, pause);
  543. };
  544. /**
  545. * Returns true if the recording is supproted and false if not.
  546. */
  547. JitsiConference.prototype.isRecordingSupported = function () {
  548. if(this.room)
  549. return this.room.isRecordingSupported();
  550. return false;
  551. };
  552. /**
  553. * Returns null if the recording is not supported, "on" if the recording started
  554. * and "off" if the recording is not started.
  555. */
  556. JitsiConference.prototype.getRecordingState = function () {
  557. if(this.room)
  558. return this.room.getRecordingState();
  559. return "off";
  560. }
  561. /**
  562. * Returns the url of the recorded video.
  563. */
  564. JitsiConference.prototype.getRecordingURL = function () {
  565. if(this.room)
  566. return this.room.getRecordingURL();
  567. return null;
  568. }
  569. /**
  570. * Starts/stops the recording
  571. */
  572. JitsiConference.prototype.toggleRecording = function (options) {
  573. if(this.room)
  574. return this.room.toggleRecording(options, function (status, error) {
  575. this.eventEmitter.emit(
  576. JitsiConferenceEvents.RECORDING_STATE_CHANGED, status, error);
  577. }.bind(this));
  578. this.eventEmitter.emit(
  579. JitsiConferenceEvents.RECORDING_STATE_CHANGED, "error",
  580. new Error("The conference is not created yet!"));
  581. }
  582. /**
  583. * Returns true if the SIP calls are supported and false otherwise
  584. */
  585. JitsiConference.prototype.isSIPCallingSupported = function () {
  586. if(this.room)
  587. return this.room.isSIPCallingSupported();
  588. return false;
  589. }
  590. /**
  591. * Dials a number.
  592. * @param number the number
  593. */
  594. JitsiConference.prototype.dial = function (number) {
  595. if(this.room)
  596. return this.room.dial(number);
  597. return new Promise(function(resolve, reject){
  598. reject(new Error("The conference is not created yet!"))});
  599. }
  600. /**
  601. * Hangup an existing call
  602. */
  603. JitsiConference.prototype.hangup = function () {
  604. if(this.room)
  605. return this.room.hangup();
  606. return new Promise(function(resolve, reject){
  607. reject(new Error("The conference is not created yet!"))});
  608. }
  609. /**
  610. * Returns the phone number for joining the conference.
  611. */
  612. JitsiConference.prototype.getPhoneNumber = function () {
  613. if(this.room)
  614. return this.room.getPhoneNumber();
  615. return null;
  616. }
  617. /**
  618. * Returns the pin for joining the conference with phone.
  619. */
  620. JitsiConference.prototype.getPhonePin = function () {
  621. if(this.room)
  622. return this.room.getPhonePin();
  623. return null;
  624. }
  625. /**
  626. * Returns the connection state for the current room. Its ice connection state
  627. * for its session.
  628. */
  629. JitsiConference.prototype.getConnectionState = function () {
  630. if(this.room)
  631. return this.room.getConnectionState();
  632. return null;
  633. }
  634. /**
  635. * Make all new participants mute their audio/video on join.
  636. * @param policy {Object} object with 2 boolean properties for video and audio:
  637. * @param {boolean} audio if audio should be muted.
  638. * @param {boolean} video if video should be muted.
  639. */
  640. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  641. if (!this.isModerator()) {
  642. return;
  643. }
  644. this.startMutedPolicy = policy;
  645. this.room.removeFromPresence("startmuted");
  646. this.room.addToPresence("startmuted", {
  647. attributes: {
  648. audio: policy.audio,
  649. video: policy.video,
  650. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  651. }
  652. });
  653. this.room.sendPresence();
  654. };
  655. /**
  656. * Returns current start muted policy
  657. * @returns {Object} with 2 proprties - audio and video.
  658. */
  659. JitsiConference.prototype.getStartMutedPolicy = function () {
  660. return this.startMutedPolicy;
  661. };
  662. /**
  663. * Check if audio is muted on join.
  664. */
  665. JitsiConference.prototype.isStartAudioMuted = function () {
  666. return this.startAudioMuted;
  667. };
  668. /**
  669. * Check if video is muted on join.
  670. */
  671. JitsiConference.prototype.isStartVideoMuted = function () {
  672. return this.startVideoMuted;
  673. };
  674. /**
  675. * Get object with internal logs.
  676. */
  677. JitsiConference.prototype.getLogs = function () {
  678. var data = this.xmpp.getJingleLog();
  679. var metadata = {};
  680. metadata.time = new Date();
  681. metadata.url = window.location.href;
  682. metadata.ua = navigator.userAgent;
  683. var log = this.xmpp.getXmppLog();
  684. if (log) {
  685. metadata.xmpp = log;
  686. }
  687. data.metadata = metadata;
  688. return data;
  689. };
  690. /**
  691. * Sends the given feedback through CallStats if enabled.
  692. *
  693. * @param overallFeedback an integer between 1 and 5 indicating the
  694. * user feedback
  695. * @param detailedFeedback detailed feedback from the user. Not yet used
  696. */
  697. JitsiConference.prototype.sendFeedback =
  698. function(overallFeedback, detailedFeedback){
  699. this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  700. }
  701. /**
  702. * Returns true if the callstats integration is enabled, otherwise returns
  703. * false.
  704. *
  705. * @returns true if the callstats integration is enabled, otherwise returns
  706. * false.
  707. */
  708. JitsiConference.prototype.isCallstatsEnabled = function () {
  709. return this.statistics.isCallstatsEnabled();
  710. }
  711. /**
  712. * Setups the listeners needed for the conference.
  713. * @param conference the conference
  714. */
  715. function setupListeners(conference) {
  716. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  717. conference.rtc.onIncommingCall(event);
  718. conference.statistics.startRemoteStats(event.peerconnection);
  719. });
  720. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  721. function (data, sid, thessrc) {
  722. var track = conference.rtc.createRemoteStream(data, sid, thessrc);
  723. if (track) {
  724. conference.onTrackAdded(track);
  725. }
  726. }
  727. );
  728. conference.rtc.addListener(RTCEvents.FAKE_VIDEO_TRACK_CREATED,
  729. function (track) {
  730. conference.onTrackAdded(track);
  731. }
  732. );
  733. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  734. function (value) {
  735. conference.rtc.setAudioMute(value);
  736. conference.isMutedByFocus = true;
  737. }
  738. );
  739. conference.room.addListener(XMPPEvents.SUBJECT_CHANGED, function (subject) {
  740. conference.eventEmitter.emit(JitsiConferenceEvents.SUBJECT_CHANGED, subject);
  741. });
  742. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  743. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  744. });
  745. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  746. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  747. });
  748. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  749. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  750. });
  751. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  752. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  753. });
  754. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  755. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  756. });
  757. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  758. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  759. });
  760. conference.room.addListener(XMPPEvents.RESERVATION_ERROR, function (code, msg) {
  761. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.RESERVATION_ERROR, code, msg);
  762. });
  763. conference.room.addListener(XMPPEvents.GRACEFUL_SHUTDOWN, function () {
  764. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  765. });
  766. conference.room.addListener(XMPPEvents.JINGLE_FATAL_ERROR, function () {
  767. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.JINGLE_FATAL_ERROR);
  768. });
  769. conference.room.addListener(XMPPEvents.MUC_DESTROYED, function (reason) {
  770. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONFERENCE_DESTROYED, reason);
  771. });
  772. conference.room.addListener(XMPPEvents.CHAT_ERROR_RECEIVED, function (err, msg) {
  773. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_ERROR, JitsiConferenceErrors.CHAT_ERROR, err, msg);
  774. });
  775. conference.room.addListener(XMPPEvents.FOCUS_DISCONNECTED, function (focus, retrySec) {
  776. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_DISCONNECTED, focus, retrySec);
  777. });
  778. // FIXME
  779. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  780. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  781. // });
  782. conference.room.addListener(XMPPEvents.KICKED, function () {
  783. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  784. });
  785. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  786. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  787. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  788. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  789. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  790. });
  791. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  792. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  793. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  794. });
  795. conference.room.addListener(XMPPEvents.RECORDING_STATE_CHANGED,
  796. function () {
  797. conference.eventEmitter.emit(
  798. JitsiConferenceEvents.RECORDING_STATE_CHANGED);
  799. });
  800. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  801. conference.eventEmitter.emit(
  802. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  803. });
  804. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  805. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  806. });
  807. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  808. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.SETUP_FAILED);
  809. });
  810. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  811. conference.authEnabled = authEnabled;
  812. conference.authIdentity = authIdentity;
  813. });
  814. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  815. var id = Strophe.getResourceFromJid(jid);
  816. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  817. });
  818. conference.room.addListener(XMPPEvents.PRESENCE_STATUS, function (jid, status) {
  819. var id = Strophe.getResourceFromJid(jid);
  820. var participant = conference.getParticipantById(id);
  821. if (!participant || participant._status === status) {
  822. return;
  823. }
  824. participant._status = status;
  825. conference.eventEmitter.emit(JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  826. });
  827. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  828. if(conference.lastDominantSpeaker !== id && conference.room) {
  829. conference.lastDominantSpeaker = id;
  830. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  831. }
  832. });
  833. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  834. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  835. });
  836. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  837. function (lastNEndpoints, endpointsEnteringLastN) {
  838. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  839. lastNEndpoints, endpointsEnteringLastN);
  840. });
  841. conference.xmpp.addListener(XMPPEvents.PASSWORD_REQUIRED, function () {
  842. conference.eventEmitter.emit(JitsiConferenceErrors.PASSWORD_REQUIRED);
  843. });
  844. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  845. function (audioMuted, videoMuted) {
  846. conference.startAudioMuted = audioMuted;
  847. conference.startVideoMuted = videoMuted;
  848. // mute existing local tracks because this is initial mute from
  849. // Jicofo
  850. conference.getLocalTracks().forEach(function (track) {
  851. if (conference.startAudioMuted && track.isAudioTrack()) {
  852. track.mute();
  853. }
  854. if (conference.startVideoMuted && track.isVideoTrack()) {
  855. track.mute();
  856. }
  857. });
  858. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  859. });
  860. conference.room.addPresenceListener("startmuted", function (data, from) {
  861. var isModerator = false;
  862. if (conference.myUserId() === from && conference.isModerator()) {
  863. isModerator = true;
  864. } else {
  865. var participant = conference.getParticipantById(from);
  866. if (participant && participant.isModerator()) {
  867. isModerator = true;
  868. }
  869. }
  870. if (!isModerator) {
  871. return;
  872. }
  873. var startAudioMuted = data.attributes.audio === 'true';
  874. var startVideoMuted = data.attributes.video === 'true';
  875. var updated = false;
  876. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  877. conference.startMutedPolicy.audio = startAudioMuted;
  878. updated = true;
  879. }
  880. if (startVideoMuted !== conference.startMutedPolicy.video) {
  881. conference.startMutedPolicy.video = startVideoMuted;
  882. updated = true;
  883. }
  884. if (updated) {
  885. conference.eventEmitter.emit(
  886. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  887. conference.startMutedPolicy
  888. );
  889. }
  890. });
  891. conference.rtc.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  892. conference.room.updateDeviceAvailability(devices);
  893. });
  894. conference.room.addPresenceListener("devices", function (data, from) {
  895. var isAudioAvailable = false;
  896. var isVideoAvailable = false;
  897. data.children.forEach(function (config) {
  898. if (config.tagName === 'audio') {
  899. isAudioAvailable = config.value === 'true';
  900. }
  901. if (config.tagName === 'video') {
  902. isVideoAvailable = config.value === 'true';
  903. }
  904. });
  905. var availableDevices;
  906. if (conference.myUserId() === from) {
  907. availableDevices = conference.availableDevices;
  908. } else {
  909. var participant = conference.getParticipantById(from);
  910. if (!participant) {
  911. return;
  912. }
  913. availableDevices = participant._availableDevices;
  914. }
  915. var updated = false;
  916. if (availableDevices.audio !== isAudioAvailable) {
  917. updated = true;
  918. availableDevices.audio = isAudioAvailable;
  919. }
  920. if (availableDevices.video !== isVideoAvailable) {
  921. updated = true;
  922. availableDevices.video = isVideoAvailable;
  923. }
  924. if (updated) {
  925. conference.eventEmitter.emit(JitsiConferenceEvents.AVAILABLE_DEVICES_CHANGED, from, availableDevices);
  926. }
  927. });
  928. if(conference.statistics) {
  929. //FIXME: Maybe remove event should not be associated with the conference.
  930. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  931. var userId = null;
  932. var jid = conference.room.getJidBySSRC(ssrc);
  933. if (!jid)
  934. return;
  935. conference.rtc.setAudioLevel(jid, level);
  936. });
  937. conference.statistics.addConnectionStatsListener(function (stats) {
  938. var ssrc2resolution = stats.resolution;
  939. var id2resolution = {};
  940. // preprocess resolutions: group by user id, skip incorrect
  941. // resolutions etc.
  942. Object.keys(ssrc2resolution).forEach(function (ssrc) {
  943. var resolution = ssrc2resolution[ssrc];
  944. if (!resolution.width || !resolution.height ||
  945. resolution.width == -1 || resolution.height == -1) {
  946. return;
  947. }
  948. var jid = conference.room.getJidBySSRC(ssrc);
  949. if (!jid) {
  950. return;
  951. }
  952. var id;
  953. if (jid === conference.room.session.me) {
  954. id = conference.myUserId();
  955. } else {
  956. id = Strophe.getResourceFromJid(jid);
  957. }
  958. if (!id) {
  959. return;
  960. }
  961. // ssrc to resolution map for user id
  962. var idResolutions = id2resolution[id] || {};
  963. idResolutions[ssrc] = resolution;
  964. id2resolution[id] = idResolutions;
  965. });
  966. stats.resolution = id2resolution;
  967. conference.eventEmitter.emit(
  968. JitsiConferenceEvents.CONNECTION_STATS, stats);
  969. });
  970. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  971. function () {
  972. conference.statistics.dispose();
  973. });
  974. conference.room.addListener(XMPPEvents.PEERCONNECTION_READY,
  975. function (session) {
  976. conference.statistics.startCallStats(
  977. session, conference.settings);
  978. });
  979. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED,
  980. function () {
  981. conference.statistics.sendSetupFailedEvent();
  982. });
  983. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  984. function (track) {
  985. if(!track.isLocal())
  986. return;
  987. var type = (track.getType() === "audio")? "audio" : "video";
  988. conference.statistics.sendMuteEvent(track.isMuted(), type);
  989. });
  990. conference.room.addListener(XMPPEvents.CREATE_OFFER_FAILED, function (e, pc) {
  991. conference.statistics.sendCreateOfferFailed(e, pc);
  992. });
  993. conference.room.addListener(XMPPEvents.CREATE_ANSWER_FAILED, function (e, pc) {
  994. conference.statistics.sendCreateAnswerFailed(e, pc);
  995. });
  996. conference.room.addListener(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  997. function (e, pc) {
  998. conference.statistics.sendSetLocalDescFailed(e, pc);
  999. }
  1000. );
  1001. conference.room.addListener(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  1002. function (e, pc) {
  1003. conference.statistics.sendSetRemoteDescFailed(e, pc);
  1004. }
  1005. );
  1006. conference.room.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  1007. function (e, pc) {
  1008. conference.statistics.sendAddIceCandidateFailed(e, pc);
  1009. }
  1010. );
  1011. }
  1012. }
  1013. module.exports = JitsiConference;