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

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