Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

JitsiConference.js 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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. * 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.selectEndpoint(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. var removedTracks = this.rtc.removeRemoteTracks(id);
  627. removedTracks.forEach(function (track) {
  628. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  629. }.bind(this));
  630. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  631. };
  632. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  633. var id = Strophe.getResourceFromJid(jid);
  634. var participant = this.getParticipantById(id);
  635. if (!participant) {
  636. return;
  637. }
  638. participant._role = role;
  639. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  640. };
  641. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  642. var id = Strophe.getResourceFromJid(jid);
  643. var participant = this.getParticipantById(id);
  644. if (!participant) {
  645. return;
  646. }
  647. if (participant._displayName === displayName)
  648. return;
  649. participant._displayName = displayName;
  650. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  651. };
  652. /**
  653. * Notifies this JitsiConference that a JitsiRemoteTrack was added (into the
  654. * ChatRoom of this JitsiConference).
  655. *
  656. * @param {JitsiRemoteTrack} track the JitsiRemoteTrack which was added to this
  657. * JitsiConference
  658. */
  659. JitsiConference.prototype.onTrackAdded = function (track) {
  660. var id = track.getParticipantId();
  661. var participant = this.getParticipantById(id);
  662. if (!participant) {
  663. return;
  664. }
  665. // Add track to JitsiParticipant.
  666. participant._tracks.push(track);
  667. var emitter = this.eventEmitter;
  668. track.addEventListener(
  669. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  670. function () {
  671. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  672. }
  673. );
  674. track.addEventListener(
  675. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  676. function (audioLevel) {
  677. emitter.emit(
  678. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  679. id,
  680. audioLevel);
  681. }
  682. );
  683. emitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  684. };
  685. /**
  686. * Handles incoming call event.
  687. */
  688. JitsiConference.prototype.onIncomingCall =
  689. function (jingleSession, jingleOffer, now) {
  690. if (!this.room.isFocus(jingleSession.peerjid)) {
  691. // Error cause this should never happen unless something is wrong!
  692. var errmsg = "Rejecting session-initiate from non-focus user: "
  693. + jingleSession.peerjid;
  694. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  695. logger.error(errmsg);
  696. return;
  697. }
  698. // Accept incoming call
  699. this.room.setJingleSession(jingleSession);
  700. this.room.connectionTimes["session.initiate"] = now;
  701. try{
  702. jingleSession.initialize(false /* initiator */,this.room);
  703. } catch (error) {
  704. GlobalOnErrorHandler.callErrorHandler(error);
  705. };
  706. this.rtc.onIncommingCall(jingleSession);
  707. // Add local Tracks to the ChatRoom
  708. this.rtc.localTracks.forEach(function(localTrack) {
  709. var ssrcInfo = null;
  710. if(localTrack.isVideoTrack() && localTrack.isMuted()) {
  711. /**
  712. * Handles issues when the stream is added before the peerconnection
  713. * is created. The peerconnection is created when second participant
  714. * enters the call. In that use case the track doesn't have
  715. * information about it's ssrcs and no jingle packets are sent. That
  716. * can cause inconsistent behavior later.
  717. *
  718. * For example:
  719. * If we mute the stream and than second participant enter it's
  720. * remote SDP won't include that track. On unmute we are not sending
  721. * any jingle packets which will brake the unmute.
  722. *
  723. * In order to solve issues like the above one here we have to
  724. * generate the ssrc information for the track .
  725. */
  726. localTrack._setSSRC(
  727. this.room.generateNewStreamSSRCInfo());
  728. ssrcInfo = {
  729. mtype: localTrack.getType(),
  730. type: "addMuted",
  731. ssrc: localTrack.ssrc,
  732. msid: localTrack.initialMSID
  733. };
  734. }
  735. try {
  736. this.room.addStream(
  737. localTrack.getOriginalStream(), function () {}, function () {},
  738. ssrcInfo, true);
  739. } catch(e) {
  740. GlobalOnErrorHandler.callErrorHandler(e);
  741. logger.error(e);
  742. }
  743. }.bind(this));
  744. jingleSession.acceptOffer(jingleOffer, null,
  745. function (error) {
  746. GlobalOnErrorHandler.callErrorHandler(error);
  747. logger.error(
  748. "Failed to accept incoming Jingle session", error);
  749. }
  750. );
  751. // Start callstats as soon as peerconnection is initialized,
  752. // do not wait for XMPPEvents.PEERCONNECTION_READY, as it may never
  753. // happen in case if user doesn't have or denied permission to
  754. // both camera and microphone.
  755. this.statistics.startCallStats(jingleSession, this.settings);
  756. this.statistics.startRemoteStats(jingleSession.peerconnection);
  757. }
  758. JitsiConference.prototype.updateDTMFSupport = function () {
  759. var somebodySupportsDTMF = false;
  760. var participants = this.getParticipants();
  761. // check if at least 1 participant supports DTMF
  762. for (var i = 0; i < participants.length; i += 1) {
  763. if (participants[i].supportsDTMF()) {
  764. somebodySupportsDTMF = true;
  765. break;
  766. }
  767. }
  768. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  769. this.somebodySupportsDTMF = somebodySupportsDTMF;
  770. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  771. }
  772. };
  773. /**
  774. * Allows to check if there is at least one user in the conference
  775. * that supports DTMF.
  776. * @returns {boolean} true if somebody supports DTMF, false otherwise
  777. */
  778. JitsiConference.prototype.isDTMFSupported = function () {
  779. return this.somebodySupportsDTMF;
  780. };
  781. /**
  782. * Returns the local user's ID
  783. * @return {string} local user's ID
  784. */
  785. JitsiConference.prototype.myUserId = function () {
  786. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  787. };
  788. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  789. if (!this.dtmfManager) {
  790. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  791. if (!connection) {
  792. logger.warn("cannot sendTones: no conneciton");
  793. return;
  794. }
  795. var tracks = this.getLocalTracks().filter(function (track) {
  796. return track.isAudioTrack();
  797. });
  798. if (!tracks.length) {
  799. logger.warn("cannot sendTones: no local audio stream");
  800. return;
  801. }
  802. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  803. }
  804. this.dtmfManager.sendTones(tones, duration, pause);
  805. };
  806. /**
  807. * Returns true if the recording is supproted and false if not.
  808. */
  809. JitsiConference.prototype.isRecordingSupported = function () {
  810. if(this.room)
  811. return this.room.isRecordingSupported();
  812. return false;
  813. };
  814. /**
  815. * Returns null if the recording is not supported, "on" if the recording started
  816. * and "off" if the recording is not started.
  817. */
  818. JitsiConference.prototype.getRecordingState = function () {
  819. return (this.room) ? this.room.getRecordingState() : undefined;
  820. }
  821. /**
  822. * Returns the url of the recorded video.
  823. */
  824. JitsiConference.prototype.getRecordingURL = function () {
  825. return (this.room) ? this.room.getRecordingURL() : null;
  826. }
  827. /**
  828. * Starts/stops the recording
  829. */
  830. JitsiConference.prototype.toggleRecording = function (options) {
  831. if(this.room)
  832. return this.room.toggleRecording(options, function (status, error) {
  833. this.eventEmitter.emit(
  834. JitsiConferenceEvents.RECORDER_STATE_CHANGED, status, error);
  835. }.bind(this));
  836. this.eventEmitter.emit(
  837. JitsiConferenceEvents.RECORDER_STATE_CHANGED, "error",
  838. new Error("The conference is not created yet!"));
  839. }
  840. /**
  841. * Returns true if the SIP calls are supported and false otherwise
  842. */
  843. JitsiConference.prototype.isSIPCallingSupported = function () {
  844. if(this.room)
  845. return this.room.isSIPCallingSupported();
  846. return false;
  847. }
  848. /**
  849. * Dials a number.
  850. * @param number the number
  851. */
  852. JitsiConference.prototype.dial = function (number) {
  853. if(this.room)
  854. return this.room.dial(number);
  855. return new Promise(function(resolve, reject){
  856. reject(new Error("The conference is not created yet!"))});
  857. }
  858. /**
  859. * Hangup an existing call
  860. */
  861. JitsiConference.prototype.hangup = function () {
  862. if(this.room)
  863. return this.room.hangup();
  864. return new Promise(function(resolve, reject){
  865. reject(new Error("The conference is not created yet!"))});
  866. }
  867. /**
  868. * Returns the phone number for joining the conference.
  869. */
  870. JitsiConference.prototype.getPhoneNumber = function () {
  871. if(this.room)
  872. return this.room.getPhoneNumber();
  873. return null;
  874. }
  875. /**
  876. * Returns the pin for joining the conference with phone.
  877. */
  878. JitsiConference.prototype.getPhonePin = function () {
  879. if(this.room)
  880. return this.room.getPhonePin();
  881. return null;
  882. }
  883. /**
  884. * Returns the connection state for the current room. Its ice connection state
  885. * for its session.
  886. */
  887. JitsiConference.prototype.getConnectionState = function () {
  888. if(this.room)
  889. return this.room.getConnectionState();
  890. return null;
  891. }
  892. /**
  893. * Make all new participants mute their audio/video on join.
  894. * @param policy {Object} object with 2 boolean properties for video and audio:
  895. * @param {boolean} audio if audio should be muted.
  896. * @param {boolean} video if video should be muted.
  897. */
  898. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  899. if (!this.isModerator()) {
  900. return;
  901. }
  902. this.startMutedPolicy = policy;
  903. this.room.removeFromPresence("startmuted");
  904. this.room.addToPresence("startmuted", {
  905. attributes: {
  906. audio: policy.audio,
  907. video: policy.video,
  908. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  909. }
  910. });
  911. this.room.sendPresence();
  912. };
  913. /**
  914. * Returns current start muted policy
  915. * @returns {Object} with 2 proprties - audio and video.
  916. */
  917. JitsiConference.prototype.getStartMutedPolicy = function () {
  918. return this.startMutedPolicy;
  919. };
  920. /**
  921. * Check if audio is muted on join.
  922. */
  923. JitsiConference.prototype.isStartAudioMuted = function () {
  924. return this.startAudioMuted;
  925. };
  926. /**
  927. * Check if video is muted on join.
  928. */
  929. JitsiConference.prototype.isStartVideoMuted = function () {
  930. return this.startVideoMuted;
  931. };
  932. /**
  933. * Get object with internal logs.
  934. */
  935. JitsiConference.prototype.getLogs = function () {
  936. var data = this.xmpp.getJingleLog();
  937. var metadata = {};
  938. metadata.time = new Date();
  939. metadata.url = window.location.href;
  940. metadata.ua = navigator.userAgent;
  941. var log = this.xmpp.getXmppLog();
  942. if (log) {
  943. metadata.xmpp = log;
  944. }
  945. data.metadata = metadata;
  946. return data;
  947. };
  948. /**
  949. * Returns measured connectionTimes.
  950. */
  951. JitsiConference.prototype.getConnectionTimes = function () {
  952. return this.room.connectionTimes;
  953. };
  954. /**
  955. * Sets a property for the local participant.
  956. */
  957. JitsiConference.prototype.setLocalParticipantProperty = function(name, value) {
  958. this.sendCommand("jitsi_participant_" + name, {value: value});
  959. };
  960. /**
  961. * Sends the given feedback through CallStats if enabled.
  962. *
  963. * @param overallFeedback an integer between 1 and 5 indicating the
  964. * user feedback
  965. * @param detailedFeedback detailed feedback from the user. Not yet used
  966. */
  967. JitsiConference.prototype.sendFeedback =
  968. function(overallFeedback, detailedFeedback){
  969. this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  970. }
  971. /**
  972. * Returns true if the callstats integration is enabled, otherwise returns
  973. * false.
  974. *
  975. * @returns true if the callstats integration is enabled, otherwise returns
  976. * false.
  977. */
  978. JitsiConference.prototype.isCallstatsEnabled = function () {
  979. return this.statistics.isCallstatsEnabled();
  980. }
  981. /**
  982. * Handles track attached to container (Calls associateStreamWithVideoTag method
  983. * from statistics module)
  984. * @param track the track
  985. * @param container the container
  986. */
  987. JitsiConference.prototype._onTrackAttach = function(track, container) {
  988. var ssrc = track.getSSRC();
  989. if (!container.id || !ssrc) {
  990. return;
  991. }
  992. this.statistics.associateStreamWithVideoTag(
  993. ssrc, track.isLocal(), track.getUsageLabel(), container.id);
  994. }
  995. /**
  996. * Reports detected audio problem with the media stream related to the passed
  997. * ssrc.
  998. * @param ssrc {string} the ssrc
  999. * NOTE: all logger.log calls are there only to be able to see the info in
  1000. * torture
  1001. */
  1002. JitsiConference.prototype._reportAudioProblem = function (ssrc) {
  1003. if(this.reportedAudioSSRCs[ssrc])
  1004. return;
  1005. var track = this.rtc.getRemoteTrackBySSRC(ssrc);
  1006. if(!track || !track.isAudioTrack())
  1007. return;
  1008. var id = track.getParticipantId();
  1009. var displayName = null;
  1010. if(id) {
  1011. var participant = this.getParticipantById(id);
  1012. if(participant) {
  1013. displayName = participant.getDisplayName();
  1014. }
  1015. }
  1016. this.reportedAudioSSRCs[ssrc] = true;
  1017. var errorContent = {
  1018. errMsg: "The audio is received but not played",
  1019. ssrc: ssrc,
  1020. jid: id,
  1021. displayName: displayName
  1022. };
  1023. logger.log("=================The audio is received but not played" +
  1024. "======================");
  1025. logger.log("ssrc: ", ssrc);
  1026. logger.log("jid: ", id);
  1027. logger.log("displayName: ", displayName);
  1028. var mstream = track.stream, mtrack = track.track;
  1029. if(mstream) {
  1030. logger.log("MediaStream:");
  1031. errorContent.MediaStream = {
  1032. active: mstream.active,
  1033. id: mstream.id
  1034. };
  1035. logger.log("active: ", mstream.active);
  1036. logger.log("id: ", mstream.id);
  1037. }
  1038. if(mtrack) {
  1039. logger.log("MediaStreamTrack:");
  1040. errorContent.MediaStreamTrack = {
  1041. enabled: mtrack.enabled,
  1042. id: mtrack.id,
  1043. label: mtrack.label,
  1044. muted: mtrack.muted
  1045. }
  1046. logger.log("enabled: ", mtrack.enabled);
  1047. logger.log("id: ", mtrack.id);
  1048. logger.log("label: ", mtrack.label);
  1049. logger.log("muted: ", mtrack.muted);
  1050. }
  1051. if(track.containers) {
  1052. errorContent.containers = [];
  1053. logger.log("Containers:");
  1054. track.containers.forEach(function (container) {
  1055. logger.log("Container:");
  1056. errorContent.containers.push({
  1057. autoplay: container.autoplay,
  1058. muted: container.muted,
  1059. src: container.src,
  1060. volume: container.volume,
  1061. id: container.id,
  1062. ended: container.ended,
  1063. paused: container.paused,
  1064. readyState: container.readyState
  1065. });
  1066. logger.log("autoplay: ", container.autoplay);
  1067. logger.log("muted: ", container.muted);
  1068. logger.log("src: ", container.src);
  1069. logger.log("volume: ", container.volume);
  1070. logger.log("id: ", container.id);
  1071. logger.log("ended: ", container.ended);
  1072. logger.log("paused: ", container.paused);
  1073. logger.log("readyState: ", container.readyState);
  1074. });
  1075. }
  1076. this.statistics.sendDetectedAudioProblem(
  1077. new Error(JSON.stringify(errorContent)));
  1078. // Prints JSON.stringify(errorContent) to be able to see all properties of
  1079. // errorContent from torture
  1080. logger.error("Audio problem detected. The audio is received but not played",
  1081. errorContent);
  1082. };
  1083. /**
  1084. * Logs an "application log" message
  1085. */
  1086. JitsiConference.prototype.sendApplicationLog = function(message) {
  1087. Statistics.sendLog(message);
  1088. };
  1089. /**
  1090. * Checks if the user identified by given <tt>mucJid</tt> is the conference
  1091. * focus.
  1092. * @param mucJid the full MUC address of the user to be checked.
  1093. * @returns {boolean} <tt>true</tt> if MUC user is the conference focus.
  1094. */
  1095. JitsiConference.prototype._isFocus = function (mucJid) {
  1096. return this.room.isFocus(mucJid);
  1097. };
  1098. /**
  1099. * Fires CONFERENCE_FAILED event with INCOMPATIBLE_SERVER_VERSIONS parameter
  1100. */
  1101. JitsiConference.prototype._fireIncompatibleVersionsEvent = function () {
  1102. this.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1103. JitsiConferenceErrors.INCOMPATIBLE_SERVER_VERSIONS);
  1104. };
  1105. module.exports = JitsiConference;