Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

JitsiConference.js 40KB

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