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

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