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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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 EventEmitter = require("events");
  7. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  8. var JitsiConferenceErrors = require("./JitsiConferenceErrors");
  9. var JitsiParticipant = require("./JitsiParticipant");
  10. var Statistics = require("./modules/statistics/statistics");
  11. var JitsiDTMFManager = require('./modules/DTMF/JitsiDTMFManager');
  12. var JitsiTrackEvents = require("./JitsiTrackEvents");
  13. var JitsiTrackErrors = require("./JitsiTrackErrors");
  14. var JitsiTrackError = require("./JitsiTrackError");
  15. var Settings = require("./modules/settings/Settings");
  16. var ComponentsVersions = require("./modules/version/ComponentsVersions");
  17. var GlobalOnErrorHandler = require("./modules/util/GlobalOnErrorHandler");
  18. var JitsiConferenceEventManager = require("./JitsiConferenceEventManager");
  19. /**
  20. * Creates a JitsiConference object with the given name and properties.
  21. * Note: this constructor is not a part of the public API (objects should be
  22. * created using JitsiConnection.createConference).
  23. * @param options.config properties / settings related to the conference that will be created.
  24. * @param options.name the name of the conference
  25. * @param options.connection the JitsiConnection object for this JitsiConference.
  26. * @constructor
  27. */
  28. function JitsiConference(options) {
  29. if(!options.name || options.name.toLowerCase() !== options.name) {
  30. var errmsg
  31. = "Invalid conference name (no conference name passed or it "
  32. + "contains invalid characters like capital letters)!";
  33. logger.error(errmsg);
  34. throw new Error(errmsg);
  35. }
  36. this.eventEmitter = new EventEmitter();
  37. this.settings = new Settings();
  38. this._init(options);
  39. this.componentsVersions = new ComponentsVersions(this);
  40. this.rtc = new RTC(this, options);
  41. this.statistics = new Statistics(this.xmpp, {
  42. callStatsID: this.options.config.callStatsID,
  43. callStatsSecret: this.options.config.callStatsSecret,
  44. disableThirdPartyRequests:
  45. this.options.config.disableThirdPartyRequests,
  46. roomName: this.options.name
  47. });
  48. this.eventManager = new JitsiConferenceEventManager(this);
  49. this._setupListeners();
  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. * Initializes the conference object properties
  67. * @param options overrides this.options
  68. */
  69. JitsiConference.prototype._init = function (options) {
  70. if(!options)
  71. options = {};
  72. if(!this.options) {
  73. this.options = options;
  74. } else {
  75. // Override config options
  76. var config = options.config || {};
  77. for(var key in config)
  78. this.options.config[key] = config[key] || this.options.config[key];
  79. }
  80. // Override connection and xmpp properties (Usefull if the connection
  81. // reloaded)
  82. this.connection = options.connection || this.connection;
  83. this.xmpp = this.connection.xmpp;
  84. this.room = this.xmpp.createRoom(this.options.name, this.options.config,
  85. this.settings);
  86. //restore previous presence options
  87. if(options.roomState) {
  88. this.room.loadState(options.roomState);
  89. }
  90. this.room.updateDeviceAvailability(RTC.getDeviceAvailability());
  91. }
  92. /**
  93. * Reloads the conference
  94. * @param options {object} options to be overriden
  95. */
  96. JitsiConference.prototype.reload = function (options) {
  97. var roomState = this.room.exportState();
  98. if(!options)
  99. options = {};
  100. options.roomState = roomState;
  101. this.leave(true);
  102. this.reinitialize(options);
  103. }
  104. /**
  105. * Reinitializes JitsiConference instance
  106. * @param options {object} options to be overriden
  107. */
  108. JitsiConference.prototype.reinitialize = function (options) {
  109. this._init(options || {});
  110. this.eventManager.setupChatRoomListeners();
  111. this.eventManager.setupStatisticsListeners();
  112. this.join();
  113. }
  114. /**
  115. * Joins the conference.
  116. * @param password {string} the password
  117. */
  118. JitsiConference.prototype.join = function (password) {
  119. if(this.room)
  120. this.room.join(password);
  121. };
  122. /**
  123. * Check if joined to the conference.
  124. */
  125. JitsiConference.prototype.isJoined = function () {
  126. return this.room && this.room.joined;
  127. };
  128. /**
  129. * Leaves the conference and calls onMemberLeft for every participant.
  130. */
  131. JitsiConference.prototype._leaveRoomAndRemoveParticipants = function () {
  132. // remove all participants
  133. this.getParticipants().forEach(function (participant) {
  134. this.onMemberLeft(participant.getJid());
  135. }.bind(this));
  136. // leave the conference
  137. if (this.room) {
  138. this.room.leave();
  139. }
  140. this.room = null;
  141. }
  142. /**
  143. * Leaves the conference.
  144. * @returns {Promise}
  145. */
  146. JitsiConference.prototype.leave = function (dontRemoveLocalTracks) {
  147. var conference = this;
  148. this.statistics.stopCallStats();
  149. this.rtc.closeAllDataChannels();
  150. if(dontRemoveLocalTracks) {
  151. this._leaveRoomAndRemoveParticipants();
  152. return Promise.resolve();
  153. }
  154. return Promise.all(
  155. conference.getLocalTracks().map(function (track) {
  156. return conference.removeTrack(track);
  157. })
  158. ).then(this._leaveRoomAndRemoveParticipants.bind(this))
  159. .catch(function (error) {
  160. logger.error(error);
  161. GlobalOnErrorHandler.callUnhandledRejectionHandler(
  162. {promise: this, reason: error});
  163. // We are proceeding with leaving the conference because room.leave may
  164. // succeed.
  165. this._leaveRoomAndRemoveParticipants();
  166. return Promise.resolve();
  167. }.bind(this));
  168. };
  169. /**
  170. * Returns name of this conference.
  171. */
  172. JitsiConference.prototype.getName = function () {
  173. return this.options.name;
  174. };
  175. /**
  176. * Check if authentication is enabled for this conference.
  177. */
  178. JitsiConference.prototype.isAuthEnabled = function () {
  179. return this.authEnabled;
  180. };
  181. /**
  182. * Check if user is logged in.
  183. */
  184. JitsiConference.prototype.isLoggedIn = function () {
  185. return !!this.authIdentity;
  186. };
  187. /**
  188. * Get authorized login.
  189. */
  190. JitsiConference.prototype.getAuthLogin = function () {
  191. return this.authIdentity;
  192. };
  193. /**
  194. * Check if external authentication is enabled for this conference.
  195. */
  196. JitsiConference.prototype.isExternalAuthEnabled = function () {
  197. return this.room && this.room.moderator.isExternalAuthEnabled();
  198. };
  199. /**
  200. * Get url for external authentication.
  201. * @param {boolean} [urlForPopup] if true then return url for login popup,
  202. * else url of login page.
  203. * @returns {Promise}
  204. */
  205. JitsiConference.prototype.getExternalAuthUrl = function (urlForPopup) {
  206. return new Promise(function (resolve, reject) {
  207. if (!this.isExternalAuthEnabled()) {
  208. reject();
  209. return;
  210. }
  211. if (urlForPopup) {
  212. this.room.moderator.getPopupLoginUrl(resolve, reject);
  213. } else {
  214. this.room.moderator.getLoginUrl(resolve, reject);
  215. }
  216. }.bind(this));
  217. };
  218. /**
  219. * Returns the local tracks.
  220. */
  221. JitsiConference.prototype.getLocalTracks = function () {
  222. if (this.rtc) {
  223. return this.rtc.localTracks.slice();
  224. } else {
  225. return [];
  226. }
  227. };
  228. /**
  229. * Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
  230. * in JitsiConferenceEvents.
  231. * @param eventId the event ID.
  232. * @param handler handler for the event.
  233. *
  234. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  235. */
  236. JitsiConference.prototype.on = function (eventId, handler) {
  237. if(this.eventEmitter)
  238. this.eventEmitter.on(eventId, handler);
  239. };
  240. /**
  241. * Removes event listener
  242. * @param eventId the event ID.
  243. * @param [handler] optional, the specific handler to unbind
  244. *
  245. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  246. */
  247. JitsiConference.prototype.off = function (eventId, handler) {
  248. if(this.eventEmitter)
  249. this.eventEmitter.removeListener(eventId, handler);
  250. };
  251. // Common aliases for event emitter
  252. JitsiConference.prototype.addEventListener = JitsiConference.prototype.on;
  253. JitsiConference.prototype.removeEventListener = JitsiConference.prototype.off;
  254. /**
  255. * Receives notifications from other participants about commands / custom events
  256. * (sent by sendCommand or sendCommandOnce methods).
  257. * @param command {String} the name of the command
  258. * @param handler {Function} handler for the command
  259. */
  260. JitsiConference.prototype.addCommandListener = function (command, handler) {
  261. if(this.room)
  262. this.room.addPresenceListener(command, handler);
  263. };
  264. /**
  265. * Removes command listener
  266. * @param command {String} the name of the command
  267. */
  268. JitsiConference.prototype.removeCommandListener = function (command) {
  269. if(this.room)
  270. this.room.removePresenceListener(command);
  271. };
  272. /**
  273. * Sends text message to the other participants in the conference
  274. * @param message the text message.
  275. */
  276. JitsiConference.prototype.sendTextMessage = function (message) {
  277. if(this.room)
  278. this.room.sendMessage(message);
  279. };
  280. /**
  281. * Send presence command.
  282. * @param name {String} the name of the command.
  283. * @param values {Object} with keys and values that will be sent.
  284. **/
  285. JitsiConference.prototype.sendCommand = function (name, values) {
  286. if(this.room) {
  287. this.room.addToPresence(name, values);
  288. this.room.sendPresence();
  289. }
  290. };
  291. /**
  292. * Send presence command one time.
  293. * @param name {String} the name of the command.
  294. * @param values {Object} with keys and values that will be sent.
  295. **/
  296. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  297. this.sendCommand(name, values);
  298. this.removeCommand(name);
  299. };
  300. /**
  301. * Removes presence command.
  302. * @param name {String} the name of the command.
  303. **/
  304. JitsiConference.prototype.removeCommand = function (name) {
  305. if(this.room)
  306. this.room.removeFromPresence(name);
  307. };
  308. /**
  309. * Sets the display name for this conference.
  310. * @param name the display name to set
  311. */
  312. JitsiConference.prototype.setDisplayName = function(name) {
  313. if(this.room){
  314. // remove previously set nickname
  315. this.room.removeFromPresence("nick");
  316. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  317. this.room.sendPresence();
  318. }
  319. };
  320. /**
  321. * Set new subject for this conference. (available only for moderator)
  322. * @param {string} subject new subject
  323. */
  324. JitsiConference.prototype.setSubject = function (subject) {
  325. if (this.room && this.isModerator()) {
  326. this.room.setSubject(subject);
  327. }
  328. };
  329. /**
  330. * Adds JitsiLocalTrack object to the conference.
  331. * @param track the JitsiLocalTrack object.
  332. * @returns {Promise<JitsiLocalTrack>}
  333. * @throws {Error} if the specified track is a video track and there is already
  334. * another video track in the conference.
  335. */
  336. JitsiConference.prototype.addTrack = function (track) {
  337. if (track.disposed) {
  338. throw new JitsiTrackError(JitsiTrackErrors.TRACK_IS_DISPOSED);
  339. }
  340. if (track.isVideoTrack()) {
  341. // Ensure there's exactly 1 local video track in the conference.
  342. var localVideoTrack = this.rtc.getLocalVideoTrack();
  343. if (localVideoTrack) {
  344. // Don't be excessively harsh and severe if the API client happens
  345. // to attempt to add the same local video track twice.
  346. if (track === localVideoTrack) {
  347. return Promise.resolve(track);
  348. } else {
  349. throw new Error(
  350. "cannot add second video track to the conference");
  351. }
  352. }
  353. }
  354. track.ssrcHandler = function (conference, ssrcMap) {
  355. if(ssrcMap[this.getMSID()]){
  356. this._setSSRC(ssrcMap[this.getMSID()]);
  357. conference.room.removeListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  358. this.ssrcHandler);
  359. }
  360. }.bind(track, this);
  361. this.room.addListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  362. track.ssrcHandler);
  363. if(track.isAudioTrack() || (track.isVideoTrack() &&
  364. track.videoType !== "desktop")) {
  365. // Report active device to statistics
  366. var devices = RTC.getCurrentlyAvailableMediaDevices();
  367. device = devices.find(function (d) {
  368. return d.kind === track.getTrack().kind + 'input'
  369. && d.label === track.getTrack().label;
  370. });
  371. if(device)
  372. Statistics.sendActiveDeviceListEvent(
  373. RTC.getEventDataForActiveDevice(device));
  374. }
  375. return new Promise(function (resolve, reject) {
  376. this.room.addStream(track.getOriginalStream(), function () {
  377. if (track.isVideoTrack()) {
  378. this.removeCommand("videoType");
  379. this.sendCommand("videoType", {
  380. value: track.videoType,
  381. attributes: {
  382. xmlns: 'http://jitsi.org/jitmeet/video'
  383. }
  384. });
  385. }
  386. this.rtc.addLocalTrack(track);
  387. if (track.startMuted) {
  388. track.mute();
  389. }
  390. // ensure that we're sharing proper "is muted" state
  391. if (track.isAudioTrack()) {
  392. this.room.setAudioMute(track.isMuted());
  393. } else {
  394. this.room.setVideoMute(track.isMuted());
  395. }
  396. track.muteHandler = this._fireMuteChangeEvent.bind(this, track);
  397. track.audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  398. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
  399. track.muteHandler);
  400. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  401. track.audioLevelHandler);
  402. track._setConference(this);
  403. // send event for starting screen sharing
  404. // FIXME: we assume we have only one screen sharing track
  405. // if we change this we need to fix this check
  406. if (track.isVideoTrack() && track.videoType === "desktop")
  407. this.statistics.sendScreenSharingEvent(true);
  408. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  409. resolve(track);
  410. }.bind(this), function (error) {
  411. reject(error);
  412. });
  413. }.bind(this));
  414. };
  415. /**
  416. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event.
  417. * @param audioLevel the audio level
  418. */
  419. JitsiConference.prototype._fireAudioLevelChangeEvent = function (audioLevel) {
  420. this.eventEmitter.emit(
  421. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  422. this.myUserId(), audioLevel);
  423. };
  424. /**
  425. * Fires TRACK_MUTE_CHANGED change conference event.
  426. * @param track the JitsiTrack object related to the event.
  427. */
  428. JitsiConference.prototype._fireMuteChangeEvent = function (track) {
  429. // check if track was muted by focus and now is unmuted by user
  430. if (this.isMutedByFocus && track.isAudioTrack() && !track.isMuted()) {
  431. this.isMutedByFocus = false;
  432. // unmute local user on server
  433. this.room.muteParticipant(this.room.myroomjid, false);
  434. }
  435. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  436. };
  437. /**
  438. * Removes JitsiLocalTrack object to the conference.
  439. * @param track the JitsiLocalTrack object.
  440. * @returns {Promise}
  441. */
  442. JitsiConference.prototype.removeTrack = function (track) {
  443. if(track.disposed)
  444. {
  445. throw new Error(JitsiTrackErrors.TRACK_IS_DISPOSED);
  446. }
  447. if(!this.room){
  448. if(this.rtc) {
  449. this.rtc.removeLocalTrack(track);
  450. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  451. }
  452. return Promise.resolve();
  453. }
  454. return new Promise(function (resolve, reject) {
  455. this.room.removeStream(track.getOriginalStream(), function(){
  456. track._setSSRC(null);
  457. track._setConference(null);
  458. this.rtc.removeLocalTrack(track);
  459. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
  460. track.muteHandler);
  461. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  462. track.audioLevelHandler);
  463. this.room.removeListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  464. track.ssrcHandler);
  465. // send event for stopping screen sharing
  466. // FIXME: we assume we have only one screen sharing track
  467. // if we change this we need to fix this check
  468. if (track.isVideoTrack() && track.videoType === "desktop")
  469. this.statistics.sendScreenSharingEvent(false);
  470. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  471. resolve();
  472. }.bind(this), function (error) {
  473. reject(error);
  474. }, {
  475. mtype: track.getType(),
  476. type: "remove",
  477. ssrc: track.ssrc});
  478. }.bind(this));
  479. };
  480. /**
  481. * Get role of the local user.
  482. * @returns {string} user role: 'moderator' or 'none'
  483. */
  484. JitsiConference.prototype.getRole = function () {
  485. return this.room.role;
  486. };
  487. /**
  488. * Check if local user is moderator.
  489. * @returns {boolean} true if local user is moderator, false otherwise.
  490. */
  491. JitsiConference.prototype.isModerator = function () {
  492. return this.room.isModerator();
  493. };
  494. /**
  495. * Set password for the room.
  496. * @param {string} password new password for the room.
  497. * @returns {Promise}
  498. */
  499. JitsiConference.prototype.lock = function (password) {
  500. if (!this.isModerator()) {
  501. return Promise.reject();
  502. }
  503. var conference = this;
  504. return new Promise(function (resolve, reject) {
  505. conference.room.lockRoom(password || "", function () {
  506. resolve();
  507. }, function (err) {
  508. reject(err);
  509. }, function () {
  510. reject(JitsiConferenceErrors.PASSWORD_NOT_SUPPORTED);
  511. });
  512. });
  513. };
  514. /**
  515. * Remove password from the room.
  516. * @returns {Promise}
  517. */
  518. JitsiConference.prototype.unlock = function () {
  519. return this.lock();
  520. };
  521. /**
  522. * Elects the participant with the given id to be the selected participant or the speaker.
  523. * @param id the identifier of the participant
  524. */
  525. JitsiConference.prototype.selectParticipant = function(participantId) {
  526. if (this.rtc) {
  527. this.rtc.selectedEndpoint(participantId);
  528. }
  529. };
  530. /**
  531. *
  532. * @param id the identifier of the participant
  533. */
  534. JitsiConference.prototype.pinParticipant = function(participantId) {
  535. if (this.rtc) {
  536. this.rtc.pinEndpoint(participantId);
  537. }
  538. };
  539. /**
  540. * Returns the list of participants for this conference.
  541. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  542. */
  543. JitsiConference.prototype.getParticipants = function() {
  544. return Object.keys(this.participants).map(function (key) {
  545. return this.participants[key];
  546. }, this);
  547. };
  548. /**
  549. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  550. * undefined if there isn't one).
  551. * @param id the id of the participant.
  552. */
  553. JitsiConference.prototype.getParticipantById = function(id) {
  554. return this.participants[id];
  555. };
  556. /**
  557. * Kick participant from this conference.
  558. * @param {string} id id of the participant to kick
  559. */
  560. JitsiConference.prototype.kickParticipant = function (id) {
  561. var participant = this.getParticipantById(id);
  562. if (!participant) {
  563. return;
  564. }
  565. this.room.kick(participant.getJid());
  566. };
  567. /**
  568. * Kick participant from this conference.
  569. * @param {string} id id of the participant to kick
  570. */
  571. JitsiConference.prototype.muteParticipant = function (id) {
  572. var participant = this.getParticipantById(id);
  573. if (!participant) {
  574. return;
  575. }
  576. this.room.muteParticipant(participant.getJid(), true);
  577. };
  578. /**
  579. * Indicates that a participant has joined the conference.
  580. *
  581. * @param jid the jid of the participant in the MUC
  582. * @param nick the display name of the participant
  583. * @param role the role of the participant in the MUC
  584. * @param isHidden indicates if this is a hidden participant (sysem participant,
  585. * for example a recorder).
  586. */
  587. JitsiConference.prototype.onMemberJoined
  588. = function (jid, nick, role, isHidden) {
  589. var id = Strophe.getResourceFromJid(jid);
  590. if (id === 'focus' || this.myUserId() === id) {
  591. return;
  592. }
  593. var participant = new JitsiParticipant(jid, this, nick, isHidden);
  594. participant._role = role;
  595. this.participants[id] = participant;
  596. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id, participant);
  597. // XXX Since disco is checked in multiple places (e.g.
  598. // modules/xmpp/strophe.jingle.js, modules/xmpp/strophe.rayo.js), check it
  599. // here as well.
  600. var disco = this.xmpp.connection.disco;
  601. if (disco) {
  602. disco.info(
  603. jid, "node", function(iq) {
  604. participant._supportsDTMF = $(iq).find(
  605. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  606. this.updateDTMFSupport();
  607. }.bind(this)
  608. );
  609. } else {
  610. // FIXME Should participant._supportsDTMF be assigned false here (and
  611. // this.updateDTMFSupport invoked)?
  612. }
  613. };
  614. JitsiConference.prototype.onMemberLeft = function (jid) {
  615. var id = Strophe.getResourceFromJid(jid);
  616. if (id === 'focus' || this.myUserId() === id) {
  617. return;
  618. }
  619. var participant = this.participants[id];
  620. delete this.participants[id];
  621. this.rtc.removeRemoteTracks(id);
  622. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  623. };
  624. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  625. var id = Strophe.getResourceFromJid(jid);
  626. var participant = this.getParticipantById(id);
  627. if (!participant) {
  628. return;
  629. }
  630. participant._role = role;
  631. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  632. };
  633. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  634. var id = Strophe.getResourceFromJid(jid);
  635. var participant = this.getParticipantById(id);
  636. if (!participant) {
  637. return;
  638. }
  639. if (participant._displayName === displayName)
  640. return;
  641. participant._displayName = displayName;
  642. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  643. };
  644. /**
  645. * Notifies this JitsiConference that a JitsiRemoteTrack was added (into the
  646. * ChatRoom of this JitsiConference).
  647. *
  648. * @param {JitsiRemoteTrack} track the JitsiRemoteTrack which was added to this
  649. * JitsiConference
  650. */
  651. JitsiConference.prototype.onTrackAdded = function (track) {
  652. var id = track.getParticipantId();
  653. var participant = this.getParticipantById(id);
  654. if (!participant) {
  655. return;
  656. }
  657. // Add track to JitsiParticipant.
  658. participant._tracks.push(track);
  659. var emitter = this.eventEmitter;
  660. track.addEventListener(
  661. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  662. function () {
  663. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  664. }
  665. );
  666. track.addEventListener(
  667. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  668. function (audioLevel) {
  669. emitter.emit(
  670. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  671. id,
  672. audioLevel);
  673. }
  674. );
  675. emitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  676. };
  677. /**
  678. * Handles incoming call event.
  679. */
  680. JitsiConference.prototype.onIncomingCall =
  681. function (jingleSession, jingleOffer, now) {
  682. if (!this.room.isFocus(jingleSession.peerjid)) {
  683. // Error cause this should never happen unless something is wrong!
  684. var errmsg = "Rejecting session-initiate from non-focus user: "
  685. + jingleSession.peerjid;
  686. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  687. logger.error(errmsg);
  688. return;
  689. }
  690. // Accept incoming call
  691. this.room.setJingleSession(jingleSession);
  692. this.room.connectionTimes["session.initiate"] = now;
  693. try{
  694. jingleSession.initialize(false /* initiator */,this.room);
  695. } catch (error) {
  696. GlobalOnErrorHandler.callErrorHandler(error);
  697. };
  698. this.rtc.onIncommingCall(jingleSession);
  699. // Add local Tracks to the ChatRoom
  700. this.rtc.localTracks.forEach(function(localTrack) {
  701. var ssrcInfo = null;
  702. if(localTrack.isVideoTrack() && localTrack.isMuted()) {
  703. /**
  704. * Handles issues when the stream is added before the peerconnection
  705. * is created. The peerconnection is created when second participant
  706. * enters the call. In that use case the track doesn't have
  707. * information about it's ssrcs and no jingle packets are sent. That
  708. * can cause inconsistent behavior later.
  709. *
  710. * For example:
  711. * If we mute the stream and than second participant enter it's
  712. * remote SDP won't include that track. On unmute we are not sending
  713. * any jingle packets which will brake the unmute.
  714. *
  715. * In order to solve issues like the above one here we have to
  716. * generate the ssrc information for the track .
  717. */
  718. localTrack._setSSRC(
  719. this.room.generateNewStreamSSRCInfo());
  720. ssrcInfo = {
  721. mtype: localTrack.getType(),
  722. type: "addMuted",
  723. ssrc: localTrack.ssrc,
  724. msid: localTrack.initialMSID
  725. };
  726. }
  727. try {
  728. this.room.addStream(
  729. localTrack.getOriginalStream(), function () {}, function () {},
  730. ssrcInfo, true);
  731. } catch(e) {
  732. GlobalOnErrorHandler.callErrorHandler(e);
  733. logger.error(e);
  734. }
  735. }.bind(this));
  736. jingleSession.acceptOffer(jingleOffer, null,
  737. function (error) {
  738. GlobalOnErrorHandler.callErrorHandler(error);
  739. logger.error(
  740. "Failed to accept incoming Jingle session", error);
  741. }
  742. );
  743. // Start callstats as soon as peerconnection is initialized,
  744. // do not wait for XMPPEvents.PEERCONNECTION_READY, as it may never
  745. // happen in case if user doesn't have or denied permission to
  746. // both camera and microphone.
  747. this.statistics.startCallStats(jingleSession, this.settings);
  748. this.statistics.startRemoteStats(jingleSession.peerconnection);
  749. }
  750. JitsiConference.prototype.updateDTMFSupport = function () {
  751. var somebodySupportsDTMF = false;
  752. var participants = this.getParticipants();
  753. // check if at least 1 participant supports DTMF
  754. for (var i = 0; i < participants.length; i += 1) {
  755. if (participants[i].supportsDTMF()) {
  756. somebodySupportsDTMF = true;
  757. break;
  758. }
  759. }
  760. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  761. this.somebodySupportsDTMF = somebodySupportsDTMF;
  762. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  763. }
  764. };
  765. /**
  766. * Allows to check if there is at least one user in the conference
  767. * that supports DTMF.
  768. * @returns {boolean} true if somebody supports DTMF, false otherwise
  769. */
  770. JitsiConference.prototype.isDTMFSupported = function () {
  771. return this.somebodySupportsDTMF;
  772. };
  773. /**
  774. * Returns the local user's ID
  775. * @return {string} local user's ID
  776. */
  777. JitsiConference.prototype.myUserId = function () {
  778. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  779. };
  780. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  781. if (!this.dtmfManager) {
  782. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  783. if (!connection) {
  784. logger.warn("cannot sendTones: no conneciton");
  785. return;
  786. }
  787. var tracks = this.getLocalTracks().filter(function (track) {
  788. return track.isAudioTrack();
  789. });
  790. if (!tracks.length) {
  791. logger.warn("cannot sendTones: no local audio stream");
  792. return;
  793. }
  794. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  795. }
  796. this.dtmfManager.sendTones(tones, duration, pause);
  797. };
  798. /**
  799. * Returns true if the recording is supproted and false if not.
  800. */
  801. JitsiConference.prototype.isRecordingSupported = function () {
  802. if(this.room)
  803. return this.room.isRecordingSupported();
  804. return false;
  805. };
  806. /**
  807. * Returns null if the recording is not supported, "on" if the recording started
  808. * and "off" if the recording is not started.
  809. */
  810. JitsiConference.prototype.getRecordingState = function () {
  811. return (this.room) ? this.room.getRecordingState() : undefined;
  812. }
  813. /**
  814. * Returns the url of the recorded video.
  815. */
  816. JitsiConference.prototype.getRecordingURL = function () {
  817. return (this.room) ? this.room.getRecordingURL() : null;
  818. }
  819. /**
  820. * Starts/stops the recording
  821. */
  822. JitsiConference.prototype.toggleRecording = function (options) {
  823. if(this.room)
  824. return this.room.toggleRecording(options, function (status, error) {
  825. this.eventEmitter.emit(
  826. JitsiConferenceEvents.RECORDER_STATE_CHANGED, status, error);
  827. }.bind(this));
  828. this.eventEmitter.emit(
  829. JitsiConferenceEvents.RECORDER_STATE_CHANGED, "error",
  830. new Error("The conference is not created yet!"));
  831. }
  832. /**
  833. * Returns true if the SIP calls are supported and false otherwise
  834. */
  835. JitsiConference.prototype.isSIPCallingSupported = function () {
  836. if(this.room)
  837. return this.room.isSIPCallingSupported();
  838. return false;
  839. }
  840. /**
  841. * Dials a number.
  842. * @param number the number
  843. */
  844. JitsiConference.prototype.dial = function (number) {
  845. if(this.room)
  846. return this.room.dial(number);
  847. return new Promise(function(resolve, reject){
  848. reject(new Error("The conference is not created yet!"))});
  849. }
  850. /**
  851. * Hangup an existing call
  852. */
  853. JitsiConference.prototype.hangup = function () {
  854. if(this.room)
  855. return this.room.hangup();
  856. return new Promise(function(resolve, reject){
  857. reject(new Error("The conference is not created yet!"))});
  858. }
  859. /**
  860. * Returns the phone number for joining the conference.
  861. */
  862. JitsiConference.prototype.getPhoneNumber = function () {
  863. if(this.room)
  864. return this.room.getPhoneNumber();
  865. return null;
  866. }
  867. /**
  868. * Returns the pin for joining the conference with phone.
  869. */
  870. JitsiConference.prototype.getPhonePin = function () {
  871. if(this.room)
  872. return this.room.getPhonePin();
  873. return null;
  874. }
  875. /**
  876. * Returns the connection state for the current room. Its ice connection state
  877. * for its session.
  878. */
  879. JitsiConference.prototype.getConnectionState = function () {
  880. if(this.room)
  881. return this.room.getConnectionState();
  882. return null;
  883. }
  884. /**
  885. * Make all new participants mute their audio/video on join.
  886. * @param policy {Object} object with 2 boolean properties for video and audio:
  887. * @param {boolean} audio if audio should be muted.
  888. * @param {boolean} video if video should be muted.
  889. */
  890. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  891. if (!this.isModerator()) {
  892. return;
  893. }
  894. this.startMutedPolicy = policy;
  895. this.room.removeFromPresence("startmuted");
  896. this.room.addToPresence("startmuted", {
  897. attributes: {
  898. audio: policy.audio,
  899. video: policy.video,
  900. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  901. }
  902. });
  903. this.room.sendPresence();
  904. };
  905. /**
  906. * Returns current start muted policy
  907. * @returns {Object} with 2 proprties - audio and video.
  908. */
  909. JitsiConference.prototype.getStartMutedPolicy = function () {
  910. return this.startMutedPolicy;
  911. };
  912. /**
  913. * Check if audio is muted on join.
  914. */
  915. JitsiConference.prototype.isStartAudioMuted = function () {
  916. return this.startAudioMuted;
  917. };
  918. /**
  919. * Check if video is muted on join.
  920. */
  921. JitsiConference.prototype.isStartVideoMuted = function () {
  922. return this.startVideoMuted;
  923. };
  924. /**
  925. * Get object with internal logs.
  926. */
  927. JitsiConference.prototype.getLogs = function () {
  928. var data = this.xmpp.getJingleLog();
  929. var metadata = {};
  930. metadata.time = new Date();
  931. metadata.url = window.location.href;
  932. metadata.ua = navigator.userAgent;
  933. var log = this.xmpp.getXmppLog();
  934. if (log) {
  935. metadata.xmpp = log;
  936. }
  937. data.metadata = metadata;
  938. return data;
  939. };
  940. /**
  941. * Returns measured connectionTimes.
  942. */
  943. JitsiConference.prototype.getConnectionTimes = function () {
  944. return this.room.connectionTimes;
  945. };
  946. /**
  947. * Sets a property for the local participant.
  948. */
  949. JitsiConference.prototype.setLocalParticipantProperty = function(name, value) {
  950. this.sendCommand("jitsi_participant_" + name, {value: value});
  951. };
  952. /**
  953. * Sends the given feedback through CallStats if enabled.
  954. *
  955. * @param overallFeedback an integer between 1 and 5 indicating the
  956. * user feedback
  957. * @param detailedFeedback detailed feedback from the user. Not yet used
  958. */
  959. JitsiConference.prototype.sendFeedback =
  960. function(overallFeedback, detailedFeedback){
  961. this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  962. }
  963. /**
  964. * Returns true if the callstats integration is enabled, otherwise returns
  965. * false.
  966. *
  967. * @returns true if the callstats integration is enabled, otherwise returns
  968. * false.
  969. */
  970. JitsiConference.prototype.isCallstatsEnabled = function () {
  971. return this.statistics.isCallstatsEnabled();
  972. }
  973. /**
  974. * Handles track attached to container (Calls associateStreamWithVideoTag method
  975. * from statistics module)
  976. * @param track the track
  977. * @param container the container
  978. */
  979. JitsiConference.prototype._onTrackAttach = function(track, container) {
  980. var ssrc = track.getSSRC();
  981. if (!container.id || !ssrc) {
  982. return;
  983. }
  984. this.statistics.associateStreamWithVideoTag(
  985. ssrc, track.isLocal(), track.getUsageLabel(), container.id);
  986. }
  987. /**
  988. * Setups the listeners needed for the conference.
  989. */
  990. JitsiConference.prototype._setupListeners = function () {
  991. this.eventManager.setupXMPPListeners();
  992. this.eventManager.setupChatRoomListeners();
  993. this.eventManager.setupRTCListeners();
  994. this.eventManager.setupStatisticsListeners();
  995. }
  996. /**
  997. * Checks if the user identified by given <tt>mucJid</tt> is the conference
  998. * focus.
  999. * @param mucJid the full MUC address of the user to be checked.
  1000. * @returns {boolean} <tt>true</tt> if MUC user is the conference focus.
  1001. */
  1002. JitsiConference.prototype._isFocus = function (mucJid) {
  1003. return this.room.isFocus(mucJid);
  1004. }
  1005. /**
  1006. * Fires CONFERENCE_FAILED event with INCOMPATIBLE_SERVER_VERSIONS parameter
  1007. */
  1008. JitsiConference.prototype._fireIncompatibleVersionsEvent = function () {
  1009. this.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1010. JitsiConferenceErrors.INCOMPATIBLE_SERVER_VERSIONS);
  1011. }
  1012. module.exports = JitsiConference;