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

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