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

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