Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

JitsiConference.js 36KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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. this.eventManager.setupChatRoomListeners();
  82. //restore previous presence options
  83. if(options.roomState) {
  84. this.room.loadState(options.roomState);
  85. }
  86. this.room.updateDeviceAvailability(RTC.getDeviceAvailability());
  87. if(!this.rtc) {
  88. this.rtc = new RTC(this, options);
  89. this.eventManager.setupRTCListeners();
  90. }
  91. if(!this.statistics) {
  92. this.statistics = new Statistics(this.xmpp, {
  93. callStatsID: this.options.config.callStatsID,
  94. callStatsSecret: this.options.config.callStatsSecret,
  95. disableThirdPartyRequests:
  96. this.options.config.disableThirdPartyRequests,
  97. roomName: this.options.name
  98. });
  99. }
  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. 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. * Logs an "application log" message
  1045. */
  1046. JitsiConference.prototype.sendApplicationLog = function(message) {
  1047. Statistics.sendLog(message);
  1048. };
  1049. /**
  1050. * Checks if the user identified by given <tt>mucJid</tt> is the conference
  1051. * focus.
  1052. * @param mucJid the full MUC address of the user to be checked.
  1053. * @returns {boolean} <tt>true</tt> if MUC user is the conference focus.
  1054. */
  1055. JitsiConference.prototype._isFocus = function (mucJid) {
  1056. return this.room.isFocus(mucJid);
  1057. };
  1058. /**
  1059. * Fires CONFERENCE_FAILED event with INCOMPATIBLE_SERVER_VERSIONS parameter
  1060. */
  1061. JitsiConference.prototype._fireIncompatibleVersionsEvent = function () {
  1062. this.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1063. JitsiConferenceErrors.INCOMPATIBLE_SERVER_VERSIONS);
  1064. };
  1065. module.exports = JitsiConference;