Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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 AuthenticationEvents = require("./service/authentication/AuthenticationEvents");
  7. var RTCEvents = require("./service/RTC/RTCEvents");
  8. var EventEmitter = require("events");
  9. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  10. var JitsiConferenceErrors = require("./JitsiConferenceErrors");
  11. var JitsiParticipant = require("./JitsiParticipant");
  12. var Statistics = require("./modules/statistics/statistics");
  13. var JitsiDTMFManager = require('./modules/DTMF/JitsiDTMFManager');
  14. var JitsiTrackEvents = require("./JitsiTrackEvents");
  15. var Settings = require("./modules/settings/Settings");
  16. /**
  17. * Creates a JitsiConference object with the given name and properties.
  18. * Note: this constructor is not a part of the public API (objects should be
  19. * created using JitsiConnection.createConference).
  20. * @param options.config properties / settings related to the conference that will be created.
  21. * @param options.name the name of the conference
  22. * @param options.connection the JitsiConnection object for this JitsiConference.
  23. * @constructor
  24. */
  25. function JitsiConference(options) {
  26. if(!options.name || options.name.toLowerCase() !== options.name) {
  27. logger.error("Invalid conference name (no conference name passed or it"
  28. + "contains invalid characters like capital letters)!");
  29. return;
  30. }
  31. this.options = options;
  32. this.connection = this.options.connection;
  33. this.xmpp = this.connection.xmpp;
  34. this.eventEmitter = new EventEmitter();
  35. var confID = this.options.name + '@' + this.xmpp.options.hosts.muc;
  36. this.settings = new Settings(confID);
  37. this.room = this.xmpp.createRoom(this.options.name, this.options.config,
  38. this.settings);
  39. this.room.updateDeviceAvailability(RTC.getDeviceAvailability());
  40. this.rtc = new RTC(this.room, options);
  41. this.statistics = new Statistics(this.xmpp, {
  42. callStatsID: this.options.config.callStatsID,
  43. callStatsSecret: this.options.config.callStatsSecret,
  44. disableThirdPartyRequests: this.options.config.disableThirdPartyRequests
  45. });
  46. setupListeners(this);
  47. JitsiMeetJS._gumFailedHandler.push(function(error) {
  48. this.statistics.sendGetUserMediaFailed(error);
  49. }.bind(this));
  50. this.participants = {};
  51. this.lastDominantSpeaker = null;
  52. this.dtmfManager = null;
  53. this.somebodySupportsDTMF = false;
  54. this.authEnabled = false;
  55. this.authIdentity;
  56. this.startAudioMuted = false;
  57. this.startVideoMuted = false;
  58. this.startMutedPolicy = {audio: false, video: false};
  59. this.availableDevices = {
  60. audio: undefined,
  61. video: undefined
  62. };
  63. this.isMutedByFocus = false;
  64. }
  65. /**
  66. * Joins the conference.
  67. * @param password {string} the password
  68. */
  69. JitsiConference.prototype.join = function (password) {
  70. if(this.room)
  71. this.room.join(password);
  72. };
  73. /**
  74. * Check if joined to the conference.
  75. */
  76. JitsiConference.prototype.isJoined = function () {
  77. return this.room && this.room.joined;
  78. };
  79. /**
  80. * Leaves the conference.
  81. */
  82. JitsiConference.prototype.leave = function () {
  83. if(this.xmpp && this.room)
  84. this.xmpp.leaveRoom(this.room.roomjid);
  85. this.room = null;
  86. };
  87. /**
  88. * Returns name of this conference.
  89. */
  90. JitsiConference.prototype.getName = function () {
  91. return this.options.name;
  92. };
  93. /**
  94. * Check if authentication is enabled for this conference.
  95. */
  96. JitsiConference.prototype.isAuthEnabled = function () {
  97. return this.authEnabled;
  98. };
  99. /**
  100. * Check if user is logged in.
  101. */
  102. JitsiConference.prototype.isLoggedIn = function () {
  103. return !!this.authIdentity;
  104. };
  105. /**
  106. * Get authorized login.
  107. */
  108. JitsiConference.prototype.getAuthLogin = function () {
  109. return this.authIdentity;
  110. };
  111. /**
  112. * Check if external authentication is enabled for this conference.
  113. */
  114. JitsiConference.prototype.isExternalAuthEnabled = function () {
  115. return this.room && this.room.moderator.isExternalAuthEnabled();
  116. };
  117. /**
  118. * Get url for external authentication.
  119. * @param {boolean} [urlForPopup] if true then return url for login popup,
  120. * else url of login page.
  121. * @returns {Promise}
  122. */
  123. JitsiConference.prototype.getExternalAuthUrl = function (urlForPopup) {
  124. return new Promise(function (resolve, reject) {
  125. if (!this.isExternalAuthEnabled()) {
  126. reject();
  127. return;
  128. }
  129. if (urlForPopup) {
  130. this.room.moderator.getPopupLoginUrl(resolve, reject);
  131. } else {
  132. this.room.moderator.getLoginUrl(resolve, reject);
  133. }
  134. }.bind(this));
  135. };
  136. /**
  137. * Returns the local tracks.
  138. */
  139. JitsiConference.prototype.getLocalTracks = function () {
  140. if (this.rtc) {
  141. return this.rtc.localStreams;
  142. } else {
  143. return [];
  144. }
  145. };
  146. /**
  147. * Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
  148. * in JitsiConferenceEvents.
  149. * @param eventId the event ID.
  150. * @param handler handler for the event.
  151. *
  152. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  153. */
  154. JitsiConference.prototype.on = function (eventId, handler) {
  155. if(this.eventEmitter)
  156. this.eventEmitter.on(eventId, handler);
  157. };
  158. /**
  159. * Removes event listener
  160. * @param eventId the event ID.
  161. * @param [handler] optional, the specific handler to unbind
  162. *
  163. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  164. */
  165. JitsiConference.prototype.off = function (eventId, handler) {
  166. if(this.eventEmitter)
  167. this.eventEmitter.removeListener(eventId, handler);
  168. };
  169. // Common aliases for event emitter
  170. JitsiConference.prototype.addEventListener = JitsiConference.prototype.on;
  171. JitsiConference.prototype.removeEventListener = JitsiConference.prototype.off;
  172. /**
  173. * Receives notifications from another participants for commands / custom events
  174. * (send by sendPresenceCommand method).
  175. * @param command {String} the name of the command
  176. * @param handler {Function} handler for the command
  177. */
  178. JitsiConference.prototype.addCommandListener = function (command, handler) {
  179. if(this.room)
  180. this.room.addPresenceListener(command, handler);
  181. };
  182. /**
  183. * Removes command listener
  184. * @param command {String} the name of the command
  185. */
  186. JitsiConference.prototype.removeCommandListener = function (command) {
  187. if(this.room)
  188. this.room.removePresenceListener(command);
  189. };
  190. /**
  191. * Sends text message to the other participants in the conference
  192. * @param message the text message.
  193. */
  194. JitsiConference.prototype.sendTextMessage = function (message) {
  195. if(this.room)
  196. this.room.sendMessage(message);
  197. };
  198. /**
  199. * Send presence command.
  200. * @param name the name of the command.
  201. * @param values Object with keys and values that will be send.
  202. **/
  203. JitsiConference.prototype.sendCommand = function (name, values) {
  204. if(this.room) {
  205. this.room.addToPresence(name, values);
  206. this.room.sendPresence();
  207. }
  208. };
  209. /**
  210. * Send presence command one time.
  211. * @param name the name of the command.
  212. * @param values Object with keys and values that will be send.
  213. **/
  214. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  215. this.sendCommand(name, values);
  216. this.removeCommand(name);
  217. };
  218. /**
  219. * Send presence command.
  220. * @param name the name of the command.
  221. * @param values Object with keys and values that will be send.
  222. * @param persistent if false the command will be sent only one time
  223. **/
  224. JitsiConference.prototype.removeCommand = function (name) {
  225. if(this.room)
  226. this.room.removeFromPresence(name);
  227. };
  228. /**
  229. * Sets the display name for this conference.
  230. * @param name the display name to set
  231. */
  232. JitsiConference.prototype.setDisplayName = function(name) {
  233. if(this.room){
  234. // remove previously set nickname
  235. this.room.removeFromPresence("nick");
  236. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  237. this.room.sendPresence();
  238. }
  239. };
  240. /**
  241. * Set new subject for this conference. (available only for moderator)
  242. * @param {string} subject new subject
  243. */
  244. JitsiConference.prototype.setSubject = function (subject) {
  245. if (this.room && this.isModerator()) {
  246. this.room.setSubject(subject);
  247. }
  248. };
  249. /**
  250. * Adds JitsiLocalTrack object to the conference.
  251. * @param track the JitsiLocalTrack object.
  252. * @returns {Promise<JitsiLocalTrack>}
  253. */
  254. JitsiConference.prototype.addTrack = function (track) {
  255. if (track.isVideoTrack()) {
  256. this.removeCommand("videoType");
  257. this.sendCommand("videoType", {
  258. value: track.videoType,
  259. attributes: {
  260. xmlns: 'http://jitsi.org/jitmeet/video'
  261. }
  262. });
  263. }
  264. return new Promise(function (resolve) {
  265. this.room.addStream(track.getOriginalStream(), function () {
  266. this.rtc.addLocalStream(track);
  267. if (track.startMuted) {
  268. track.mute();
  269. }
  270. track.muteHandler = this._fireMuteChangeEvent.bind(this, track);
  271. track.stopHandler = this.removeTrack.bind(this, track);
  272. track.audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  273. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
  274. track.muteHandler);
  275. track.addEventListener(JitsiTrackEvents.TRACK_STOPPED,
  276. track.stopHandler);
  277. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  278. track.audioLevelHandler);
  279. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  280. resolve(track);
  281. }.bind(this));
  282. }.bind(this));
  283. };
  284. /**
  285. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event.
  286. * @param audioLevel the audio level
  287. */
  288. JitsiConference.prototype._fireAudioLevelChangeEvent = function (audioLevel) {
  289. this.eventEmitter.emit(
  290. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  291. this.myUserId(), audioLevel);
  292. };
  293. /**
  294. * Fires TRACK_MUTE_CHANGED change conference event.
  295. * @param track the JitsiTrack object related to the event.
  296. */
  297. JitsiConference.prototype._fireMuteChangeEvent = function (track) {
  298. // check if track was muted by focus and now is unmuted by user
  299. if (this.isMutedByFocus && track.isAudioTrack() && !track.isMuted()) {
  300. this.isMutedByFocus = false;
  301. // unmute local user on server
  302. this.room.muteParticipant(this.room.myroomjid, false);
  303. }
  304. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  305. };
  306. /**
  307. * Removes JitsiLocalTrack object to the conference.
  308. * @param track the JitsiLocalTrack object.
  309. * @returns {Promise}
  310. */
  311. JitsiConference.prototype.removeTrack = function (track) {
  312. if(!this.room){
  313. if(this.rtc) {
  314. this.rtc.removeLocalStream(track);
  315. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  316. }
  317. return Promise.resolve();
  318. }
  319. return new Promise(function (resolve) {
  320. this.room.removeStream(track.getOriginalStream(), function(){
  321. this.rtc.removeLocalStream(track);
  322. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, track.muteHandler);
  323. track.removeEventListener(JitsiTrackEvents.TRACK_STOPPED, track.stopHandler);
  324. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, track.audioLevelHandler);
  325. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  326. resolve();
  327. }.bind(this));
  328. }.bind(this));
  329. };
  330. /**
  331. * Get role of the local user.
  332. * @returns {string} user role: 'moderator' or 'none'
  333. */
  334. JitsiConference.prototype.getRole = function () {
  335. return this.room.role;
  336. };
  337. /**
  338. * Check if local user is moderator.
  339. * @returns {boolean} true if local user is moderator, false otherwise.
  340. */
  341. JitsiConference.prototype.isModerator = function () {
  342. return this.room.isModerator();
  343. };
  344. /**
  345. * Set password for the room.
  346. * @param {string} password new password for the room.
  347. * @returns {Promise}
  348. */
  349. JitsiConference.prototype.lock = function (password) {
  350. if (!this.isModerator()) {
  351. return Promise.reject();
  352. }
  353. var conference = this;
  354. return new Promise(function (resolve, reject) {
  355. conference.room.lockRoom(password || "", function () {
  356. resolve();
  357. }, function (err) {
  358. reject(err);
  359. }, function () {
  360. reject(JitsiConferenceErrors.PASSWORD_NOT_SUPPORTED);
  361. });
  362. });
  363. };
  364. /**
  365. * Remove password from the room.
  366. * @returns {Promise}
  367. */
  368. JitsiConference.prototype.unlock = function () {
  369. return this.lock();
  370. };
  371. /**
  372. * Elects the participant with the given id to be the selected participant or the speaker.
  373. * @param id the identifier of the participant
  374. */
  375. JitsiConference.prototype.selectParticipant = function(participantId) {
  376. if (this.rtc) {
  377. this.rtc.selectedEndpoint(participantId);
  378. }
  379. };
  380. /**
  381. *
  382. * @param id the identifier of the participant
  383. */
  384. JitsiConference.prototype.pinParticipant = function(participantId) {
  385. if (this.rtc) {
  386. this.rtc.pinEndpoint(participantId);
  387. }
  388. };
  389. /**
  390. * Returns the list of participants for this conference.
  391. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  392. */
  393. JitsiConference.prototype.getParticipants = function() {
  394. return Object.keys(this.participants).map(function (key) {
  395. return this.participants[key];
  396. }, this);
  397. };
  398. /**
  399. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  400. * undefined if there isn't one).
  401. * @param id the id of the participant.
  402. */
  403. JitsiConference.prototype.getParticipantById = function(id) {
  404. return this.participants[id];
  405. };
  406. /**
  407. * Kick participant from this conference.
  408. * @param {string} id id of the participant to kick
  409. */
  410. JitsiConference.prototype.kickParticipant = function (id) {
  411. var participant = this.getParticipantById(id);
  412. if (!participant) {
  413. return;
  414. }
  415. this.room.kick(participant.getJid());
  416. };
  417. /**
  418. * Kick participant from this conference.
  419. * @param {string} id id of the participant to kick
  420. */
  421. JitsiConference.prototype.muteParticipant = function (id) {
  422. var participant = this.getParticipantById(id);
  423. if (!participant) {
  424. return;
  425. }
  426. this.room.muteParticipant(participant.getJid(), true);
  427. };
  428. JitsiConference.prototype.onMemberJoined = function (jid, nick, role) {
  429. var id = Strophe.getResourceFromJid(jid);
  430. if (id === 'focus' || this.myUserId() === id) {
  431. return;
  432. }
  433. var participant = new JitsiParticipant(jid, this, nick);
  434. participant._role = role;
  435. this.participants[id] = participant;
  436. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id, participant);
  437. this.xmpp.connection.disco.info(
  438. jid, "node", function(iq) {
  439. participant._supportsDTMF = $(iq).find(
  440. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  441. this.updateDTMFSupport();
  442. }.bind(this)
  443. );
  444. };
  445. JitsiConference.prototype.onMemberLeft = function (jid) {
  446. var id = Strophe.getResourceFromJid(jid);
  447. if (id === 'focus' || this.myUserId() === id) {
  448. return;
  449. }
  450. var participant = this.participants[id];
  451. delete this.participants[id];
  452. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  453. };
  454. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  455. var id = Strophe.getResourceFromJid(jid);
  456. var participant = this.getParticipantById(id);
  457. if (!participant) {
  458. return;
  459. }
  460. participant._role = role;
  461. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  462. };
  463. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  464. var id = Strophe.getResourceFromJid(jid);
  465. var participant = this.getParticipantById(id);
  466. if (!participant) {
  467. return;
  468. }
  469. participant._displayName = displayName;
  470. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  471. };
  472. JitsiConference.prototype.onTrackAdded = function (track) {
  473. var id = track.getParticipantId();
  474. var participant = this.getParticipantById(id);
  475. if (!participant) {
  476. return;
  477. }
  478. // add track to JitsiParticipant
  479. participant._tracks.push(track);
  480. var emitter = this.eventEmitter;
  481. track.addEventListener(
  482. JitsiTrackEvents.TRACK_STOPPED,
  483. function () {
  484. // remove track from JitsiParticipant
  485. var pos = participant._tracks.indexOf(track);
  486. if (pos > -1) {
  487. participant._tracks.splice(pos, 1);
  488. }
  489. emitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  490. }
  491. );
  492. track.addEventListener(
  493. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  494. function () {
  495. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  496. }
  497. );
  498. track.addEventListener(
  499. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  500. function (audioLevel) {
  501. emitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  502. }
  503. );
  504. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  505. };
  506. JitsiConference.prototype.updateDTMFSupport = function () {
  507. var somebodySupportsDTMF = false;
  508. var participants = this.getParticipants();
  509. // check if at least 1 participant supports DTMF
  510. for (var i = 0; i < participants.length; i += 1) {
  511. if (participants[i].supportsDTMF()) {
  512. somebodySupportsDTMF = true;
  513. break;
  514. }
  515. }
  516. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  517. this.somebodySupportsDTMF = somebodySupportsDTMF;
  518. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  519. }
  520. };
  521. /**
  522. * Allows to check if there is at least one user in the conference
  523. * that supports DTMF.
  524. * @returns {boolean} true if somebody supports DTMF, false otherwise
  525. */
  526. JitsiConference.prototype.isDTMFSupported = function () {
  527. return this.somebodySupportsDTMF;
  528. };
  529. /**
  530. * Returns the local user's ID
  531. * @return {string} local user's ID
  532. */
  533. JitsiConference.prototype.myUserId = function () {
  534. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  535. };
  536. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  537. if (!this.dtmfManager) {
  538. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  539. if (!connection) {
  540. logger.warn("cannot sendTones: no conneciton");
  541. return;
  542. }
  543. var tracks = this.getLocalTracks().filter(function (track) {
  544. return track.isAudioTrack();
  545. });
  546. if (!tracks.length) {
  547. logger.warn("cannot sendTones: no local audio stream");
  548. return;
  549. }
  550. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  551. }
  552. this.dtmfManager.sendTones(tones, duration, pause);
  553. };
  554. /**
  555. * Returns true if the recording is supproted and false if not.
  556. */
  557. JitsiConference.prototype.isRecordingSupported = function () {
  558. if(this.room)
  559. return this.room.isRecordingSupported();
  560. return false;
  561. };
  562. /**
  563. * Returns null if the recording is not supported, "on" if the recording started
  564. * and "off" if the recording is not started.
  565. */
  566. JitsiConference.prototype.getRecordingState = function () {
  567. if(this.room)
  568. return this.room.getRecordingState();
  569. return "off";
  570. }
  571. /**
  572. * Returns the url of the recorded video.
  573. */
  574. JitsiConference.prototype.getRecordingURL = function () {
  575. if(this.room)
  576. return this.room.getRecordingURL();
  577. return null;
  578. }
  579. /**
  580. * Starts/stops the recording
  581. */
  582. JitsiConference.prototype.toggleRecording = function (options) {
  583. if(this.room)
  584. return this.room.toggleRecording(options, function (status, error) {
  585. this.eventEmitter.emit(
  586. JitsiConferenceEvents.RECORDING_STATE_CHANGED, status, error);
  587. }.bind(this));
  588. this.eventEmitter.emit(
  589. JitsiConferenceEvents.RECORDING_STATE_CHANGED, "error",
  590. new Error("The conference is not created yet!"));
  591. }
  592. /**
  593. * Returns true if the SIP calls are supported and false otherwise
  594. */
  595. JitsiConference.prototype.isSIPCallingSupported = function () {
  596. if(this.room)
  597. return this.room.isSIPCallingSupported();
  598. return false;
  599. }
  600. /**
  601. * Dials a number.
  602. * @param number the number
  603. */
  604. JitsiConference.prototype.dial = function (number) {
  605. if(this.room)
  606. return this.room.dial(number);
  607. return new Promise(function(resolve, reject){
  608. reject(new Error("The conference is not created yet!"))});
  609. }
  610. /**
  611. * Hangup an existing call
  612. */
  613. JitsiConference.prototype.hangup = function () {
  614. if(this.room)
  615. return this.room.hangup();
  616. return new Promise(function(resolve, reject){
  617. reject(new Error("The conference is not created yet!"))});
  618. }
  619. /**
  620. * Returns the phone number for joining the conference.
  621. */
  622. JitsiConference.prototype.getPhoneNumber = function () {
  623. if(this.room)
  624. return this.room.getPhoneNumber();
  625. return null;
  626. }
  627. /**
  628. * Returns the pin for joining the conference with phone.
  629. */
  630. JitsiConference.prototype.getPhonePin = function () {
  631. if(this.room)
  632. return this.room.getPhonePin();
  633. return null;
  634. }
  635. /**
  636. * Returns the connection state for the current room. Its ice connection state
  637. * for its session.
  638. */
  639. JitsiConference.prototype.getConnectionState = function () {
  640. if(this.room)
  641. return this.room.getConnectionState();
  642. return null;
  643. }
  644. /**
  645. * Make all new participants mute their audio/video on join.
  646. * @param policy {Object} object with 2 boolean properties for video and audio:
  647. * @param {boolean} audio if audio should be muted.
  648. * @param {boolean} video if video should be muted.
  649. */
  650. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  651. if (!this.isModerator()) {
  652. return;
  653. }
  654. this.startMutedPolicy = policy;
  655. this.room.removeFromPresence("startmuted");
  656. this.room.addToPresence("startmuted", {
  657. attributes: {
  658. audio: policy.audio,
  659. video: policy.video,
  660. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  661. }
  662. });
  663. this.room.sendPresence();
  664. };
  665. /**
  666. * Returns current start muted policy
  667. * @returns {Object} with 2 proprties - audio and video.
  668. */
  669. JitsiConference.prototype.getStartMutedPolicy = function () {
  670. return this.startMutedPolicy;
  671. };
  672. /**
  673. * Check if audio is muted on join.
  674. */
  675. JitsiConference.prototype.isStartAudioMuted = function () {
  676. return this.startAudioMuted;
  677. };
  678. /**
  679. * Check if video is muted on join.
  680. */
  681. JitsiConference.prototype.isStartVideoMuted = function () {
  682. return this.startVideoMuted;
  683. };
  684. /**
  685. * Get object with internal logs.
  686. */
  687. JitsiConference.prototype.getLogs = function () {
  688. var data = this.xmpp.getJingleLog();
  689. var metadata = {};
  690. metadata.time = new Date();
  691. metadata.url = window.location.href;
  692. metadata.ua = navigator.userAgent;
  693. var log = this.xmpp.getXmppLog();
  694. if (log) {
  695. metadata.xmpp = log;
  696. }
  697. data.metadata = metadata;
  698. return data;
  699. };
  700. /**
  701. * Sends the given feedback through CallStats if enabled.
  702. *
  703. * @param overallFeedback an integer between 1 and 5 indicating the
  704. * user feedback
  705. * @param detailedFeedback detailed feedback from the user. Not yet used
  706. */
  707. JitsiConference.prototype.sendFeedback =
  708. function(overallFeedback, detailedFeedback){
  709. this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  710. }
  711. /**
  712. * Returns true if the callstats integration is enabled, otherwise returns
  713. * false.
  714. *
  715. * @returns true if the callstats integration is enabled, otherwise returns
  716. * false.
  717. */
  718. JitsiConference.prototype.isCallstatsEnabled = function () {
  719. return this.statistics.isCallstatsEnabled();
  720. }
  721. /**
  722. * Setups the listeners needed for the conference.
  723. * @param conference the conference
  724. */
  725. function setupListeners(conference) {
  726. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  727. conference.rtc.onIncommingCall(event);
  728. conference.statistics.startRemoteStats(event.peerconnection);
  729. });
  730. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  731. function (data, sid, thessrc) {
  732. var track = conference.rtc.createRemoteStream(data, sid, thessrc);
  733. if (track) {
  734. conference.onTrackAdded(track);
  735. }
  736. }
  737. );
  738. conference.rtc.addListener(RTCEvents.FAKE_VIDEO_TRACK_CREATED,
  739. function (track) {
  740. conference.onTrackAdded(track);
  741. }
  742. );
  743. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  744. function (value) {
  745. conference.rtc.setAudioMute(value);
  746. conference.isMutedByFocus = true;
  747. }
  748. );
  749. conference.room.addListener(XMPPEvents.SUBJECT_CHANGED, function (subject) {
  750. conference.eventEmitter.emit(JitsiConferenceEvents.SUBJECT_CHANGED, subject);
  751. });
  752. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  753. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  754. });
  755. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  756. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  757. });
  758. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  759. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  760. });
  761. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  762. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  763. });
  764. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  765. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  766. });
  767. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  768. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  769. });
  770. conference.room.addListener(XMPPEvents.RESERVATION_ERROR, function (code, msg) {
  771. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.RESERVATION_ERROR, code, msg);
  772. });
  773. conference.room.addListener(XMPPEvents.GRACEFUL_SHUTDOWN, function () {
  774. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  775. });
  776. conference.room.addListener(XMPPEvents.JINGLE_FATAL_ERROR, function () {
  777. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.JINGLE_FATAL_ERROR);
  778. });
  779. conference.room.addListener(XMPPEvents.MUC_DESTROYED, function (reason) {
  780. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONFERENCE_DESTROYED, reason);
  781. });
  782. conference.room.addListener(XMPPEvents.CHAT_ERROR_RECEIVED, function (err, msg) {
  783. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_ERROR, JitsiConferenceErrors.CHAT_ERROR, err, msg);
  784. });
  785. conference.room.addListener(XMPPEvents.FOCUS_DISCONNECTED, function (focus, retrySec) {
  786. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_DISCONNECTED, focus, retrySec);
  787. });
  788. // FIXME
  789. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  790. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  791. // });
  792. conference.room.addListener(XMPPEvents.KICKED, function () {
  793. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  794. });
  795. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  796. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  797. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  798. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  799. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  800. });
  801. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  802. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  803. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  804. });
  805. conference.room.addListener(XMPPEvents.RECORDING_STATE_CHANGED,
  806. function () {
  807. conference.eventEmitter.emit(
  808. JitsiConferenceEvents.RECORDING_STATE_CHANGED);
  809. });
  810. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  811. conference.eventEmitter.emit(
  812. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  813. });
  814. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  815. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  816. });
  817. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  818. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.SETUP_FAILED);
  819. });
  820. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  821. conference.authEnabled = authEnabled;
  822. conference.authIdentity = authIdentity;
  823. });
  824. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  825. var id = Strophe.getResourceFromJid(jid);
  826. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  827. });
  828. conference.room.addListener(XMPPEvents.PRESENCE_STATUS, function (jid, status) {
  829. var id = Strophe.getResourceFromJid(jid);
  830. var participant = conference.getParticipantById(id);
  831. if (!participant || participant._status === status) {
  832. return;
  833. }
  834. participant._status = status;
  835. conference.eventEmitter.emit(JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  836. });
  837. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  838. if(conference.lastDominantSpeaker !== id && conference.room) {
  839. conference.lastDominantSpeaker = id;
  840. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  841. }
  842. });
  843. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  844. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  845. });
  846. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  847. function (lastNEndpoints, endpointsEnteringLastN) {
  848. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  849. lastNEndpoints, endpointsEnteringLastN);
  850. });
  851. conference.xmpp.addListener(XMPPEvents.PASSWORD_REQUIRED, function () {
  852. conference.eventEmitter.emit(JitsiConferenceErrors.PASSWORD_REQUIRED);
  853. });
  854. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  855. function (audioMuted, videoMuted) {
  856. conference.startAudioMuted = audioMuted;
  857. conference.startVideoMuted = videoMuted;
  858. // mute existing local tracks because this is initial mute from
  859. // Jicofo
  860. conference.getLocalTracks().forEach(function (track) {
  861. if (conference.startAudioMuted && track.isAudioTrack()) {
  862. track.mute();
  863. }
  864. if (conference.startVideoMuted && track.isVideoTrack()) {
  865. track.mute();
  866. }
  867. });
  868. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  869. });
  870. conference.room.addPresenceListener("startmuted", function (data, from) {
  871. var isModerator = false;
  872. if (conference.myUserId() === from && conference.isModerator()) {
  873. isModerator = true;
  874. } else {
  875. var participant = conference.getParticipantById(from);
  876. if (participant && participant.isModerator()) {
  877. isModerator = true;
  878. }
  879. }
  880. if (!isModerator) {
  881. return;
  882. }
  883. var startAudioMuted = data.attributes.audio === 'true';
  884. var startVideoMuted = data.attributes.video === 'true';
  885. var updated = false;
  886. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  887. conference.startMutedPolicy.audio = startAudioMuted;
  888. updated = true;
  889. }
  890. if (startVideoMuted !== conference.startMutedPolicy.video) {
  891. conference.startMutedPolicy.video = startVideoMuted;
  892. updated = true;
  893. }
  894. if (updated) {
  895. conference.eventEmitter.emit(
  896. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  897. conference.startMutedPolicy
  898. );
  899. }
  900. });
  901. conference.rtc.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  902. conference.room.updateDeviceAvailability(devices);
  903. });
  904. conference.room.addPresenceListener("devices", function (data, from) {
  905. var isAudioAvailable = false;
  906. var isVideoAvailable = false;
  907. data.children.forEach(function (config) {
  908. if (config.tagName === 'audio') {
  909. isAudioAvailable = config.value === 'true';
  910. }
  911. if (config.tagName === 'video') {
  912. isVideoAvailable = config.value === 'true';
  913. }
  914. });
  915. var availableDevices;
  916. if (conference.myUserId() === from) {
  917. availableDevices = conference.availableDevices;
  918. } else {
  919. var participant = conference.getParticipantById(from);
  920. if (!participant) {
  921. return;
  922. }
  923. availableDevices = participant._availableDevices;
  924. }
  925. var updated = false;
  926. if (availableDevices.audio !== isAudioAvailable) {
  927. updated = true;
  928. availableDevices.audio = isAudioAvailable;
  929. }
  930. if (availableDevices.video !== isVideoAvailable) {
  931. updated = true;
  932. availableDevices.video = isVideoAvailable;
  933. }
  934. if (updated) {
  935. conference.eventEmitter.emit(JitsiConferenceEvents.AVAILABLE_DEVICES_CHANGED, from, availableDevices);
  936. }
  937. });
  938. if(conference.statistics) {
  939. //FIXME: Maybe remove event should not be associated with the conference.
  940. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  941. var userId = null;
  942. var jid = conference.room.getJidBySSRC(ssrc);
  943. if (!jid)
  944. return;
  945. conference.rtc.setAudioLevel(jid, level);
  946. });
  947. conference.statistics.addConnectionStatsListener(function (stats) {
  948. var ssrc2resolution = stats.resolution;
  949. var id2resolution = {};
  950. // preprocess resolutions: group by user id, skip incorrect
  951. // resolutions etc.
  952. Object.keys(ssrc2resolution).forEach(function (ssrc) {
  953. var resolution = ssrc2resolution[ssrc];
  954. if (!resolution.width || !resolution.height ||
  955. resolution.width == -1 || resolution.height == -1) {
  956. return;
  957. }
  958. var jid = conference.room.getJidBySSRC(ssrc);
  959. if (!jid) {
  960. return;
  961. }
  962. var id;
  963. if (jid === conference.room.session.me) {
  964. id = conference.myUserId();
  965. } else {
  966. id = Strophe.getResourceFromJid(jid);
  967. }
  968. if (!id) {
  969. return;
  970. }
  971. // ssrc to resolution map for user id
  972. var idResolutions = id2resolution[id] || {};
  973. idResolutions[ssrc] = resolution;
  974. id2resolution[id] = idResolutions;
  975. });
  976. stats.resolution = id2resolution;
  977. conference.eventEmitter.emit(
  978. JitsiConferenceEvents.CONNECTION_STATS, stats);
  979. });
  980. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  981. function () {
  982. conference.statistics.dispose();
  983. });
  984. conference.room.addListener(XMPPEvents.PEERCONNECTION_READY,
  985. function (session) {
  986. conference.statistics.startCallStats(
  987. session, conference.settings);
  988. });
  989. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED,
  990. function () {
  991. conference.statistics.sendSetupFailedEvent();
  992. });
  993. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  994. function (track) {
  995. if(!track.isLocal())
  996. return;
  997. var type = (track.getType() === "audio")? "audio" : "video";
  998. conference.statistics.sendMuteEvent(track.isMuted(), type);
  999. });
  1000. conference.room.addListener(XMPPEvents.CREATE_OFFER_FAILED, function (e, pc) {
  1001. conference.statistics.sendCreateOfferFailed(e, pc);
  1002. });
  1003. conference.room.addListener(XMPPEvents.CREATE_ANSWER_FAILED, function (e, pc) {
  1004. conference.statistics.sendCreateAnswerFailed(e, pc);
  1005. });
  1006. conference.room.addListener(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  1007. function (e, pc) {
  1008. conference.statistics.sendSetLocalDescFailed(e, pc);
  1009. }
  1010. );
  1011. conference.room.addListener(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  1012. function (e, pc) {
  1013. conference.statistics.sendSetRemoteDescFailed(e, pc);
  1014. }
  1015. );
  1016. conference.room.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  1017. function (e, pc) {
  1018. conference.statistics.sendAddIceCandidateFailed(e, pc);
  1019. }
  1020. );
  1021. }
  1022. }
  1023. module.exports = JitsiConference;