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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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. disableThirdPartyRequests:
  95. this.options.config.disableThirdPartyRequests,
  96. roomName: this.options.name
  97. });
  98. }
  99. this.eventManager.setupChatRoomListeners();
  100. // Always add listeners because on reload we are executing leave and the
  101. // listeners are removed from statistics module.
  102. this.eventManager.setupStatisticsListeners();
  103. }
  104. /**
  105. * Reloads the conference
  106. * @param options {object} options to be overriden
  107. */
  108. JitsiConference.prototype._reload = function (options) {
  109. var roomState = this.room.exportState();
  110. if(!options)
  111. options = {};
  112. options.roomState = roomState;
  113. this.leave(true);
  114. this._init(options);
  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. * Clear JitsiLocalTrack properties and listeners.
  444. * @param track the JitsiLocalTrack object.
  445. */
  446. JitsiConference.prototype.onTrackRemoved = function (track) {
  447. track._setSSRC(null);
  448. track._setConference(null);
  449. this.rtc.removeLocalTrack(track);
  450. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
  451. track.muteHandler);
  452. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  453. track.audioLevelHandler);
  454. this.room.removeListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  455. track.ssrcHandler);
  456. // send event for stopping screen sharing
  457. // FIXME: we assume we have only one screen sharing track
  458. // if we change this we need to fix this check
  459. if (track.isVideoTrack() && track.videoType === "desktop")
  460. this.statistics.sendScreenSharingEvent(false);
  461. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  462. }
  463. /**
  464. * Removes JitsiLocalTrack object to the conference.
  465. * @param track the JitsiLocalTrack object.
  466. * @returns {Promise}
  467. */
  468. JitsiConference.prototype.removeTrack = function (track) {
  469. if (track.disposed) {
  470. return Promise.reject(
  471. new JitsiTrackError(JitsiTrackErrors.TRACK_IS_DISPOSED));
  472. }
  473. if(!this.room){
  474. if(this.rtc) {
  475. this.onTrackRemoved(track);
  476. }
  477. return Promise.resolve();
  478. }
  479. return new Promise(function (resolve, reject) {
  480. this.room.removeStream(track.getOriginalStream(), function(){
  481. this.onTrackRemoved(track);
  482. resolve();
  483. }.bind(this), function (error) {
  484. reject(error);
  485. }, {
  486. mtype: track.getType(),
  487. type: "remove",
  488. ssrc: track.ssrc});
  489. }.bind(this));
  490. };
  491. /**
  492. * Get role of the local user.
  493. * @returns {string} user role: 'moderator' or 'none'
  494. */
  495. JitsiConference.prototype.getRole = function () {
  496. return this.room.role;
  497. };
  498. /**
  499. * Check if local user is moderator.
  500. * @returns {boolean} true if local user is moderator, false otherwise.
  501. */
  502. JitsiConference.prototype.isModerator = function () {
  503. return this.room.isModerator();
  504. };
  505. /**
  506. * Set password for the room.
  507. * @param {string} password new password for the room.
  508. * @returns {Promise}
  509. */
  510. JitsiConference.prototype.lock = function (password) {
  511. if (!this.isModerator()) {
  512. return Promise.reject();
  513. }
  514. var conference = this;
  515. return new Promise(function (resolve, reject) {
  516. conference.room.lockRoom(password || "", function () {
  517. resolve();
  518. }, function (err) {
  519. reject(err);
  520. }, function () {
  521. reject(JitsiConferenceErrors.PASSWORD_NOT_SUPPORTED);
  522. });
  523. });
  524. };
  525. /**
  526. * Remove password from the room.
  527. * @returns {Promise}
  528. */
  529. JitsiConference.prototype.unlock = function () {
  530. return this.lock();
  531. };
  532. /**
  533. * Elects the participant with the given id to be the selected participant or the speaker.
  534. * @param id the identifier of the participant
  535. */
  536. JitsiConference.prototype.selectParticipant = function(participantId) {
  537. if (this.rtc) {
  538. this.rtc.selectEndpoint(participantId);
  539. }
  540. };
  541. /**
  542. *
  543. * @param id the identifier of the participant
  544. */
  545. JitsiConference.prototype.pinParticipant = function(participantId) {
  546. if (this.rtc) {
  547. this.rtc.pinEndpoint(participantId);
  548. }
  549. };
  550. /**
  551. * Returns the list of participants for this conference.
  552. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  553. */
  554. JitsiConference.prototype.getParticipants = function() {
  555. return Object.keys(this.participants).map(function (key) {
  556. return this.participants[key];
  557. }, this);
  558. };
  559. /**
  560. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  561. * undefined if there isn't one).
  562. * @param id the id of the participant.
  563. */
  564. JitsiConference.prototype.getParticipantById = function(id) {
  565. return this.participants[id];
  566. };
  567. /**
  568. * Kick participant from this conference.
  569. * @param {string} id id of the participant to kick
  570. */
  571. JitsiConference.prototype.kickParticipant = function (id) {
  572. var participant = this.getParticipantById(id);
  573. if (!participant) {
  574. return;
  575. }
  576. this.room.kick(participant.getJid());
  577. };
  578. /**
  579. * Kick participant from this conference.
  580. * @param {string} id id of the participant to kick
  581. */
  582. JitsiConference.prototype.muteParticipant = function (id) {
  583. var participant = this.getParticipantById(id);
  584. if (!participant) {
  585. return;
  586. }
  587. this.room.muteParticipant(participant.getJid(), true);
  588. };
  589. /**
  590. * Indicates that a participant has joined the conference.
  591. *
  592. * @param jid the jid of the participant in the MUC
  593. * @param nick the display name of the participant
  594. * @param role the role of the participant in the MUC
  595. * @param isHidden indicates if this is a hidden participant (sysem participant,
  596. * for example a recorder).
  597. */
  598. JitsiConference.prototype.onMemberJoined
  599. = function (jid, nick, role, isHidden) {
  600. var id = Strophe.getResourceFromJid(jid);
  601. if (id === 'focus' || this.myUserId() === id) {
  602. return;
  603. }
  604. var participant = new JitsiParticipant(jid, this, nick, isHidden);
  605. participant._role = role;
  606. this.participants[id] = participant;
  607. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id, participant);
  608. // XXX Since disco is checked in multiple places (e.g.
  609. // modules/xmpp/strophe.jingle.js, modules/xmpp/strophe.rayo.js), check it
  610. // here as well.
  611. var disco = this.xmpp.connection.disco;
  612. if (disco) {
  613. disco.info(
  614. jid, "node", function(iq) {
  615. participant._supportsDTMF = $(iq).find(
  616. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  617. this.updateDTMFSupport();
  618. }.bind(this)
  619. );
  620. } else {
  621. // FIXME Should participant._supportsDTMF be assigned false here (and
  622. // this.updateDTMFSupport invoked)?
  623. }
  624. };
  625. JitsiConference.prototype.onMemberLeft = function (jid) {
  626. var id = Strophe.getResourceFromJid(jid);
  627. if (id === 'focus' || this.myUserId() === id) {
  628. return;
  629. }
  630. var participant = this.participants[id];
  631. delete this.participants[id];
  632. var removedTracks = this.rtc.removeRemoteTracks(id);
  633. removedTracks.forEach(function (track) {
  634. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  635. }.bind(this));
  636. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  637. };
  638. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  639. var id = Strophe.getResourceFromJid(jid);
  640. var participant = this.getParticipantById(id);
  641. if (!participant) {
  642. return;
  643. }
  644. participant._role = role;
  645. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  646. };
  647. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  648. var id = Strophe.getResourceFromJid(jid);
  649. var participant = this.getParticipantById(id);
  650. if (!participant) {
  651. return;
  652. }
  653. if (participant._displayName === displayName)
  654. return;
  655. participant._displayName = displayName;
  656. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  657. };
  658. /**
  659. * Notifies this JitsiConference that a JitsiRemoteTrack was added (into the
  660. * ChatRoom of this JitsiConference).
  661. *
  662. * @param {JitsiRemoteTrack} track the JitsiRemoteTrack which was added to this
  663. * JitsiConference
  664. */
  665. JitsiConference.prototype.onTrackAdded = function (track) {
  666. var id = track.getParticipantId();
  667. var participant = this.getParticipantById(id);
  668. if (!participant) {
  669. return;
  670. }
  671. // Add track to JitsiParticipant.
  672. participant._tracks.push(track);
  673. var emitter = this.eventEmitter;
  674. track.addEventListener(
  675. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  676. function () {
  677. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  678. }
  679. );
  680. track.addEventListener(
  681. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  682. function (audioLevel) {
  683. emitter.emit(
  684. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  685. id,
  686. audioLevel);
  687. }
  688. );
  689. emitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  690. };
  691. /**
  692. * Handles incoming call event.
  693. */
  694. JitsiConference.prototype.onIncomingCall =
  695. function (jingleSession, jingleOffer, now) {
  696. if (!this.room.isFocus(jingleSession.peerjid)) {
  697. // Error cause this should never happen unless something is wrong!
  698. var errmsg = "Rejecting session-initiate from non-focus user: "
  699. + jingleSession.peerjid;
  700. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  701. logger.error(errmsg);
  702. return;
  703. }
  704. // Accept incoming call
  705. this.room.setJingleSession(jingleSession);
  706. this.room.connectionTimes["session.initiate"] = now;
  707. try{
  708. jingleSession.initialize(false /* initiator */,this.room);
  709. } catch (error) {
  710. GlobalOnErrorHandler.callErrorHandler(error);
  711. };
  712. this.rtc.onIncommingCall(jingleSession);
  713. // Add local Tracks to the ChatRoom
  714. this.rtc.localTracks.forEach(function(localTrack) {
  715. var ssrcInfo = null;
  716. if(localTrack.isVideoTrack() && localTrack.isMuted()) {
  717. /**
  718. * Handles issues when the stream is added before the peerconnection
  719. * is created. The peerconnection is created when second participant
  720. * enters the call. In that use case the track doesn't have
  721. * information about it's ssrcs and no jingle packets are sent. That
  722. * can cause inconsistent behavior later.
  723. *
  724. * For example:
  725. * If we mute the stream and than second participant enter it's
  726. * remote SDP won't include that track. On unmute we are not sending
  727. * any jingle packets which will brake the unmute.
  728. *
  729. * In order to solve issues like the above one here we have to
  730. * generate the ssrc information for the track .
  731. */
  732. localTrack._setSSRC(
  733. this.room.generateNewStreamSSRCInfo());
  734. ssrcInfo = {
  735. mtype: localTrack.getType(),
  736. type: "addMuted",
  737. ssrc: localTrack.ssrc,
  738. msid: localTrack.initialMSID
  739. };
  740. }
  741. try {
  742. this.room.addStream(
  743. localTrack.getOriginalStream(), function () {}, function () {},
  744. ssrcInfo, true);
  745. } catch(e) {
  746. GlobalOnErrorHandler.callErrorHandler(e);
  747. logger.error(e);
  748. }
  749. }.bind(this));
  750. jingleSession.acceptOffer(jingleOffer, null,
  751. function (error) {
  752. GlobalOnErrorHandler.callErrorHandler(error);
  753. logger.error(
  754. "Failed to accept incoming Jingle session", error);
  755. }
  756. );
  757. // Start callstats as soon as peerconnection is initialized,
  758. // do not wait for XMPPEvents.PEERCONNECTION_READY, as it may never
  759. // happen in case if user doesn't have or denied permission to
  760. // both camera and microphone.
  761. this.statistics.startCallStats(jingleSession, this.settings);
  762. this.statistics.startRemoteStats(jingleSession.peerconnection);
  763. }
  764. JitsiConference.prototype.updateDTMFSupport = function () {
  765. var somebodySupportsDTMF = false;
  766. var participants = this.getParticipants();
  767. // check if at least 1 participant supports DTMF
  768. for (var i = 0; i < participants.length; i += 1) {
  769. if (participants[i].supportsDTMF()) {
  770. somebodySupportsDTMF = true;
  771. break;
  772. }
  773. }
  774. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  775. this.somebodySupportsDTMF = somebodySupportsDTMF;
  776. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  777. }
  778. };
  779. /**
  780. * Allows to check if there is at least one user in the conference
  781. * that supports DTMF.
  782. * @returns {boolean} true if somebody supports DTMF, false otherwise
  783. */
  784. JitsiConference.prototype.isDTMFSupported = function () {
  785. return this.somebodySupportsDTMF;
  786. };
  787. /**
  788. * Returns the local user's ID
  789. * @return {string} local user's ID
  790. */
  791. JitsiConference.prototype.myUserId = function () {
  792. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  793. };
  794. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  795. if (!this.dtmfManager) {
  796. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  797. if (!connection) {
  798. logger.warn("cannot sendTones: no conneciton");
  799. return;
  800. }
  801. var tracks = this.getLocalTracks().filter(function (track) {
  802. return track.isAudioTrack();
  803. });
  804. if (!tracks.length) {
  805. logger.warn("cannot sendTones: no local audio stream");
  806. return;
  807. }
  808. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  809. }
  810. this.dtmfManager.sendTones(tones, duration, pause);
  811. };
  812. /**
  813. * Returns true if the recording is supproted and false if not.
  814. */
  815. JitsiConference.prototype.isRecordingSupported = function () {
  816. if(this.room)
  817. return this.room.isRecordingSupported();
  818. return false;
  819. };
  820. /**
  821. * Returns null if the recording is not supported, "on" if the recording started
  822. * and "off" if the recording is not started.
  823. */
  824. JitsiConference.prototype.getRecordingState = function () {
  825. return (this.room) ? this.room.getRecordingState() : undefined;
  826. }
  827. /**
  828. * Returns the url of the recorded video.
  829. */
  830. JitsiConference.prototype.getRecordingURL = function () {
  831. return (this.room) ? this.room.getRecordingURL() : null;
  832. }
  833. /**
  834. * Starts/stops the recording
  835. */
  836. JitsiConference.prototype.toggleRecording = function (options) {
  837. if(this.room)
  838. return this.room.toggleRecording(options, function (status, error) {
  839. this.eventEmitter.emit(
  840. JitsiConferenceEvents.RECORDER_STATE_CHANGED, status, error);
  841. }.bind(this));
  842. this.eventEmitter.emit(
  843. JitsiConferenceEvents.RECORDER_STATE_CHANGED, "error",
  844. new Error("The conference is not created yet!"));
  845. }
  846. /**
  847. * Returns true if the SIP calls are supported and false otherwise
  848. */
  849. JitsiConference.prototype.isSIPCallingSupported = function () {
  850. if(this.room)
  851. return this.room.isSIPCallingSupported();
  852. return false;
  853. }
  854. /**
  855. * Dials a number.
  856. * @param number the number
  857. */
  858. JitsiConference.prototype.dial = function (number) {
  859. if(this.room)
  860. return this.room.dial(number);
  861. return new Promise(function(resolve, reject){
  862. reject(new Error("The conference is not created yet!"))});
  863. }
  864. /**
  865. * Hangup an existing call
  866. */
  867. JitsiConference.prototype.hangup = function () {
  868. if(this.room)
  869. return this.room.hangup();
  870. return new Promise(function(resolve, reject){
  871. reject(new Error("The conference is not created yet!"))});
  872. }
  873. /**
  874. * Returns the phone number for joining the conference.
  875. */
  876. JitsiConference.prototype.getPhoneNumber = function () {
  877. if(this.room)
  878. return this.room.getPhoneNumber();
  879. return null;
  880. }
  881. /**
  882. * Returns the pin for joining the conference with phone.
  883. */
  884. JitsiConference.prototype.getPhonePin = function () {
  885. if(this.room)
  886. return this.room.getPhonePin();
  887. return null;
  888. }
  889. /**
  890. * Returns the connection state for the current room. Its ice connection state
  891. * for its session.
  892. */
  893. JitsiConference.prototype.getConnectionState = function () {
  894. if(this.room)
  895. return this.room.getConnectionState();
  896. return null;
  897. }
  898. /**
  899. * Make all new participants mute their audio/video on join.
  900. * @param policy {Object} object with 2 boolean properties for video and audio:
  901. * @param {boolean} audio if audio should be muted.
  902. * @param {boolean} video if video should be muted.
  903. */
  904. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  905. if (!this.isModerator()) {
  906. return;
  907. }
  908. this.startMutedPolicy = policy;
  909. this.room.removeFromPresence("startmuted");
  910. this.room.addToPresence("startmuted", {
  911. attributes: {
  912. audio: policy.audio,
  913. video: policy.video,
  914. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  915. }
  916. });
  917. this.room.sendPresence();
  918. };
  919. /**
  920. * Returns current start muted policy
  921. * @returns {Object} with 2 proprties - audio and video.
  922. */
  923. JitsiConference.prototype.getStartMutedPolicy = function () {
  924. return this.startMutedPolicy;
  925. };
  926. /**
  927. * Check if audio is muted on join.
  928. */
  929. JitsiConference.prototype.isStartAudioMuted = function () {
  930. return this.startAudioMuted;
  931. };
  932. /**
  933. * Check if video is muted on join.
  934. */
  935. JitsiConference.prototype.isStartVideoMuted = function () {
  936. return this.startVideoMuted;
  937. };
  938. /**
  939. * Get object with internal logs.
  940. */
  941. JitsiConference.prototype.getLogs = function () {
  942. var data = this.xmpp.getJingleLog();
  943. var metadata = {};
  944. metadata.time = new Date();
  945. metadata.url = window.location.href;
  946. metadata.ua = navigator.userAgent;
  947. var log = this.xmpp.getXmppLog();
  948. if (log) {
  949. metadata.xmpp = log;
  950. }
  951. data.metadata = metadata;
  952. return data;
  953. };
  954. /**
  955. * Returns measured connectionTimes.
  956. */
  957. JitsiConference.prototype.getConnectionTimes = function () {
  958. return this.room.connectionTimes;
  959. };
  960. /**
  961. * Sets a property for the local participant.
  962. */
  963. JitsiConference.prototype.setLocalParticipantProperty = function(name, value) {
  964. this.sendCommand("jitsi_participant_" + name, {value: value});
  965. };
  966. /**
  967. * Sends the given feedback through CallStats if enabled.
  968. *
  969. * @param overallFeedback an integer between 1 and 5 indicating the
  970. * user feedback
  971. * @param detailedFeedback detailed feedback from the user. Not yet used
  972. */
  973. JitsiConference.prototype.sendFeedback =
  974. function(overallFeedback, detailedFeedback){
  975. this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  976. }
  977. /**
  978. * Returns true if the callstats integration is enabled, otherwise returns
  979. * false.
  980. *
  981. * @returns true if the callstats integration is enabled, otherwise returns
  982. * false.
  983. */
  984. JitsiConference.prototype.isCallstatsEnabled = function () {
  985. return this.statistics.isCallstatsEnabled();
  986. }
  987. /**
  988. * Handles track attached to container (Calls associateStreamWithVideoTag method
  989. * from statistics module)
  990. * @param track the track
  991. * @param container the container
  992. */
  993. JitsiConference.prototype._onTrackAttach = function(track, container) {
  994. var ssrc = track.getSSRC();
  995. if (!container.id || !ssrc) {
  996. return;
  997. }
  998. this.statistics.associateStreamWithVideoTag(
  999. ssrc, track.isLocal(), track.getUsageLabel(), container.id);
  1000. }
  1001. /**
  1002. * Reports detected audio problem with the media stream related to the passed
  1003. * ssrc.
  1004. * @param ssrc {string} the ssrc
  1005. * NOTE: all logger.log calls are there only to be able to see the info in
  1006. * torture
  1007. */
  1008. JitsiConference.prototype._reportAudioProblem = function (ssrc) {
  1009. if(this.reportedAudioSSRCs[ssrc])
  1010. return;
  1011. var track = this.rtc.getRemoteTrackBySSRC(ssrc);
  1012. if(!track || !track.isAudioTrack())
  1013. return;
  1014. var id = track.getParticipantId();
  1015. var displayName = null;
  1016. if(id) {
  1017. var participant = this.getParticipantById(id);
  1018. if(participant) {
  1019. displayName = participant.getDisplayName();
  1020. }
  1021. }
  1022. this.reportedAudioSSRCs[ssrc] = true;
  1023. var errorContent = {
  1024. errMsg: "The audio is received but not played",
  1025. ssrc: ssrc,
  1026. jid: id,
  1027. displayName: displayName
  1028. };
  1029. logger.log("=================The audio is received but not played" +
  1030. "======================");
  1031. logger.log("ssrc: ", ssrc);
  1032. logger.log("jid: ", id);
  1033. logger.log("displayName: ", displayName);
  1034. var mstream = track.stream, mtrack = track.track;
  1035. if(mstream) {
  1036. logger.log("MediaStream:");
  1037. errorContent.MediaStream = {
  1038. active: mstream.active,
  1039. id: mstream.id
  1040. };
  1041. logger.log("active: ", mstream.active);
  1042. logger.log("id: ", mstream.id);
  1043. }
  1044. if(mtrack) {
  1045. logger.log("MediaStreamTrack:");
  1046. errorContent.MediaStreamTrack = {
  1047. enabled: mtrack.enabled,
  1048. id: mtrack.id,
  1049. label: mtrack.label,
  1050. muted: mtrack.muted
  1051. }
  1052. logger.log("enabled: ", mtrack.enabled);
  1053. logger.log("id: ", mtrack.id);
  1054. logger.log("label: ", mtrack.label);
  1055. logger.log("muted: ", mtrack.muted);
  1056. }
  1057. if(track.containers) {
  1058. errorContent.containers = [];
  1059. logger.log("Containers:");
  1060. track.containers.forEach(function (container) {
  1061. logger.log("Container:");
  1062. errorContent.containers.push({
  1063. autoplay: container.autoplay,
  1064. muted: container.muted,
  1065. src: container.src,
  1066. volume: container.volume,
  1067. id: container.id,
  1068. ended: container.ended,
  1069. paused: container.paused,
  1070. readyState: container.readyState
  1071. });
  1072. logger.log("autoplay: ", container.autoplay);
  1073. logger.log("muted: ", container.muted);
  1074. logger.log("src: ", container.src);
  1075. logger.log("volume: ", container.volume);
  1076. logger.log("id: ", container.id);
  1077. logger.log("ended: ", container.ended);
  1078. logger.log("paused: ", container.paused);
  1079. logger.log("readyState: ", container.readyState);
  1080. });
  1081. }
  1082. this.statistics.sendDetectedAudioProblem(
  1083. new Error(JSON.stringify(errorContent)));
  1084. // Prints JSON.stringify(errorContent) to be able to see all properties of
  1085. // errorContent from torture
  1086. logger.error("Audio problem detected. The audio is received but not played",
  1087. errorContent);
  1088. };
  1089. /**
  1090. * Logs an "application log" message
  1091. */
  1092. JitsiConference.prototype.sendApplicationLog = function(message) {
  1093. Statistics.sendLog(message);
  1094. };
  1095. /**
  1096. * Checks if the user identified by given <tt>mucJid</tt> is the conference
  1097. * focus.
  1098. * @param mucJid the full MUC address of the user to be checked.
  1099. * @returns {boolean} <tt>true</tt> if MUC user is the conference focus.
  1100. */
  1101. JitsiConference.prototype._isFocus = function (mucJid) {
  1102. return this.room.isFocus(mucJid);
  1103. };
  1104. /**
  1105. * Fires CONFERENCE_FAILED event with INCOMPATIBLE_SERVER_VERSIONS parameter
  1106. */
  1107. JitsiConference.prototype._fireIncompatibleVersionsEvent = function () {
  1108. this.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1109. JitsiConferenceErrors.INCOMPATIBLE_SERVER_VERSIONS);
  1110. };
  1111. /**
  1112. * Sends message via the datachannels.
  1113. * @param to {string} the id of the endpoint that should receive the message.
  1114. * If "" the message will be sent to all participants.
  1115. * @param payload {object} the payload of the message.
  1116. */
  1117. JitsiConference.prototype.sendEndpointMessage = function (to, payload) {
  1118. if(this.rtc) {
  1119. this.rtc.sendDataChannelMessage(to, payload);
  1120. }
  1121. }
  1122. /**
  1123. * Sends broadcast message via the datachannels.
  1124. * @param payload {object} the payload of the message.
  1125. */
  1126. JitsiConference.prototype.broadcastEndpointMessage = function (payload) {
  1127. this.sendEndpointMessage("", payload);
  1128. }
  1129. module.exports = JitsiConference;