Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JitsiConference.js 40KB

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