選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

JitsiConference.js 40KB

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