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

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