You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JitsiConference.js 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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. /**
  16. * Creates a JitsiConference object with the given name and properties.
  17. * Note: this constructor is not a part of the public API (objects should be
  18. * created using JitsiConnection.createConference).
  19. * @param options.config properties / settings related to the conference that will be created.
  20. * @param options.name the name of the conference
  21. * @param options.connection the JitsiConnection object for this JitsiConference.
  22. * @constructor
  23. */
  24. function JitsiConference(options) {
  25. if(!options.name || options.name.toLowerCase() !== options.name) {
  26. logger.error("Invalid conference name (no conference name passed or it"
  27. + "contains invalid characters like capital letters)!");
  28. return;
  29. }
  30. this.options = options;
  31. this.connection = this.options.connection;
  32. this.xmpp = this.connection.xmpp;
  33. this.eventEmitter = new EventEmitter();
  34. this.room = this.xmpp.createRoom(this.options.name, this.options.config);
  35. this.room.updateDeviceAvailability(RTC.getDeviceAvailability());
  36. this.rtc = new RTC(this.room, options);
  37. if(!RTC.options.disableAudioLevels)
  38. this.statistics = new Statistics();
  39. setupListeners(this);
  40. this.participants = {};
  41. this.lastDominantSpeaker = null;
  42. this.dtmfManager = null;
  43. this.somebodySupportsDTMF = false;
  44. this.authEnabled = false;
  45. this.authIdentity;
  46. this.startAudioMuted = false;
  47. this.startVideoMuted = false;
  48. this.startMutedPolicy = {audio: false, video: false};
  49. }
  50. /**
  51. * Joins the conference.
  52. * @param password {string} the password
  53. */
  54. JitsiConference.prototype.join = function (password) {
  55. if(this.room)
  56. this.room.join(password);
  57. };
  58. /**
  59. * Check if joined to the conference.
  60. */
  61. JitsiConference.prototype.isJoined = function () {
  62. return this.room && this.room.joined;
  63. };
  64. /**
  65. * Leaves the conference.
  66. */
  67. JitsiConference.prototype.leave = function () {
  68. if(this.xmpp && this.room)
  69. this.xmpp.leaveRoom(this.room.roomjid);
  70. this.room = null;
  71. };
  72. /**
  73. * Returns name of this conference.
  74. */
  75. JitsiConference.prototype.getName = function () {
  76. return this.options.name;
  77. };
  78. /**
  79. * Check if authentication is enabled for this conference.
  80. */
  81. JitsiConference.prototype.isAuthEnabled = function () {
  82. return this.authEnabled;
  83. };
  84. /**
  85. * Check if user is logged in.
  86. */
  87. JitsiConference.prototype.isLoggedIn = function () {
  88. return !!this.authIdentity;
  89. };
  90. /**
  91. * Get authorized login.
  92. */
  93. JitsiConference.prototype.getAuthLogin = function () {
  94. return this.authIdentity;
  95. };
  96. /**
  97. * Check if external authentication is enabled for this conference.
  98. */
  99. JitsiConference.prototype.isExternalAuthEnabled = function () {
  100. return this.room && this.room.moderator.isExternalAuthEnabled();
  101. };
  102. /**
  103. * Get url for external authentication.
  104. * @param {boolean} [urlForPopup] if true then return url for login popup,
  105. * else url of login page.
  106. * @returns {Promise}
  107. */
  108. JitsiConference.prototype.getExternalAuthUrl = function (urlForPopup) {
  109. return new Promise(function (resolve, reject) {
  110. if (!this.isExternalAuthEnabled()) {
  111. reject();
  112. return;
  113. }
  114. if (urlForPopup) {
  115. this.room.moderator.getPopupLoginUrl(resolve, reject);
  116. } else {
  117. this.room.moderator.getLoginUrl(resolve, reject);
  118. }
  119. }.bind(this));
  120. };
  121. /**
  122. * Returns the local tracks.
  123. */
  124. JitsiConference.prototype.getLocalTracks = function () {
  125. if (this.rtc) {
  126. return this.rtc.localStreams;
  127. } else {
  128. return [];
  129. }
  130. };
  131. /**
  132. * Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
  133. * in JitsiConferenceEvents.
  134. * @param eventId the event ID.
  135. * @param handler handler for the event.
  136. *
  137. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  138. */
  139. JitsiConference.prototype.on = function (eventId, handler) {
  140. if(this.eventEmitter)
  141. this.eventEmitter.on(eventId, handler);
  142. };
  143. /**
  144. * Removes event listener
  145. * @param eventId the event ID.
  146. * @param [handler] optional, the specific handler to unbind
  147. *
  148. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  149. */
  150. JitsiConference.prototype.off = function (eventId, handler) {
  151. if(this.eventEmitter)
  152. this.eventEmitter.removeListener(eventId, handler);
  153. };
  154. // Common aliases for event emitter
  155. JitsiConference.prototype.addEventListener = JitsiConference.prototype.on;
  156. JitsiConference.prototype.removeEventListener = JitsiConference.prototype.off;
  157. /**
  158. * Receives notifications from another participants for commands / custom events
  159. * (send by sendPresenceCommand method).
  160. * @param command {String} the name of the command
  161. * @param handler {Function} handler for the command
  162. */
  163. JitsiConference.prototype.addCommandListener = function (command, handler) {
  164. if(this.room)
  165. this.room.addPresenceListener(command, handler);
  166. };
  167. /**
  168. * Removes command listener
  169. * @param command {String} the name of the command
  170. */
  171. JitsiConference.prototype.removeCommandListener = function (command) {
  172. if(this.room)
  173. this.room.removePresenceListener(command);
  174. };
  175. /**
  176. * Sends text message to the other participants in the conference
  177. * @param message the text message.
  178. */
  179. JitsiConference.prototype.sendTextMessage = function (message) {
  180. if(this.room)
  181. this.room.sendMessage(message);
  182. };
  183. /**
  184. * Send presence command.
  185. * @param name the name of the command.
  186. * @param values Object with keys and values that will be send.
  187. **/
  188. JitsiConference.prototype.sendCommand = function (name, values) {
  189. if(this.room) {
  190. this.room.addToPresence(name, values);
  191. this.room.sendPresence();
  192. }
  193. };
  194. /**
  195. * Send presence command one time.
  196. * @param name the name of the command.
  197. * @param values Object with keys and values that will be send.
  198. **/
  199. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  200. this.sendCommand(name, values);
  201. this.removeCommand(name);
  202. };
  203. /**
  204. * Send presence command.
  205. * @param name the name of the command.
  206. * @param values Object with keys and values that will be send.
  207. * @param persistent if false the command will be sent only one time
  208. **/
  209. JitsiConference.prototype.removeCommand = function (name) {
  210. if(this.room)
  211. this.room.removeFromPresence(name);
  212. };
  213. /**
  214. * Sets the display name for this conference.
  215. * @param name the display name to set
  216. */
  217. JitsiConference.prototype.setDisplayName = function(name) {
  218. if(this.room){
  219. // remove previously set nickname
  220. this.room.removeFromPresence("nick");
  221. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  222. this.room.sendPresence();
  223. }
  224. };
  225. /**
  226. * Adds JitsiLocalTrack object to the conference.
  227. * @param track the JitsiLocalTrack object.
  228. */
  229. JitsiConference.prototype.addTrack = function (track) {
  230. this.room.addStream(track.getOriginalStream(), function () {
  231. this.rtc.addLocalStream(track);
  232. if (track.startMuted) {
  233. track.mute();
  234. }
  235. var muteHandler = this._fireMuteChangeEvent.bind(this, track);
  236. var stopHandler = this.removeTrack.bind(this, track);
  237. var audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  238. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, muteHandler);
  239. track.addEventListener(JitsiTrackEvents.TRACK_STOPPED, stopHandler);
  240. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, audioLevelHandler);
  241. this.addEventListener(JitsiConferenceEvents.TRACK_REMOVED, function (someTrack) {
  242. if (someTrack !== track) {
  243. return;
  244. }
  245. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, muteHandler);
  246. track.removeEventListener(JitsiTrackEvents.TRACK_STOPPED, stopHandler);
  247. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, audioLevelHandler);
  248. });
  249. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  250. }.bind(this));
  251. };
  252. /**
  253. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event.
  254. * @param audioLevel the audio level
  255. */
  256. JitsiConference.prototype._fireAudioLevelChangeEvent = function (audioLevel) {
  257. this.eventEmitter.emit(
  258. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  259. this.myUserId(), audioLevel);
  260. };
  261. /**
  262. * Fires TRACK_MUTE_CHANGED change conference event.
  263. * @param track the JitsiTrack object related to the event.
  264. */
  265. JitsiConference.prototype._fireMuteChangeEvent = function (track) {
  266. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  267. };
  268. /**
  269. * Removes JitsiLocalTrack object to the conference.
  270. * @param track the JitsiLocalTrack object.
  271. */
  272. JitsiConference.prototype.removeTrack = function (track) {
  273. if(!this.room){
  274. if(this.rtc)
  275. this.rtc.removeLocalStream(track);
  276. return;
  277. }
  278. this.room.removeStream(track.getOriginalStream(), function(){
  279. this.rtc.removeLocalStream(track);
  280. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  281. }.bind(this));
  282. };
  283. /**
  284. * Get role of the local user.
  285. * @returns {string} user role: 'moderator' or 'none'
  286. */
  287. JitsiConference.prototype.getRole = function () {
  288. return this.room.role;
  289. };
  290. /**
  291. * Check if local user is moderator.
  292. * @returns {boolean} true if local user is moderator, false otherwise.
  293. */
  294. JitsiConference.prototype.isModerator = function () {
  295. return this.room.isModerator();
  296. };
  297. /**
  298. * Set password for the room.
  299. * @param {string} password new password for the room.
  300. * @returns {Promise}
  301. */
  302. JitsiConference.prototype.lock = function (password) {
  303. if (!this.isModerator()) {
  304. return Promise.reject();
  305. }
  306. var conference = this;
  307. return new Promise(function (resolve, reject) {
  308. conference.room.lockRoom(password || "", function () {
  309. resolve();
  310. }, function (err) {
  311. reject(err);
  312. }, function () {
  313. reject(JitsiConferenceErrors.PASSWORD_NOT_SUPPORTED);
  314. });
  315. });
  316. };
  317. /**
  318. * Remove password from the room.
  319. * @returns {Promise}
  320. */
  321. JitsiConference.prototype.unlock = function () {
  322. return this.lock();
  323. };
  324. /**
  325. * Elects the participant with the given id to be the selected participant or the speaker.
  326. * @param id the identifier of the participant
  327. */
  328. JitsiConference.prototype.selectParticipant = function(participantId) {
  329. if (this.rtc) {
  330. this.rtc.selectedEndpoint(participantId);
  331. }
  332. };
  333. /**
  334. *
  335. * @param id the identifier of the participant
  336. */
  337. JitsiConference.prototype.pinParticipant = function(participantId) {
  338. if (this.rtc) {
  339. this.rtc.pinEndpoint(participantId);
  340. }
  341. };
  342. /**
  343. * Returns the list of participants for this conference.
  344. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  345. */
  346. JitsiConference.prototype.getParticipants = function() {
  347. return Object.keys(this.participants).map(function (key) {
  348. return this.participants[key];
  349. }, this);
  350. };
  351. /**
  352. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  353. * undefined if there isn't one).
  354. * @param id the id of the participant.
  355. */
  356. JitsiConference.prototype.getParticipantById = function(id) {
  357. return this.participants[id];
  358. };
  359. /**
  360. * Kick participant from this conference.
  361. * @param {string} id id of the participant to kick
  362. */
  363. JitsiConference.prototype.kickParticipant = function (id) {
  364. var participant = this.getParticipantById(id);
  365. if (!participant) {
  366. return;
  367. }
  368. this.room.kick(participant.getJid());
  369. };
  370. /**
  371. * Kick participant from this conference.
  372. * @param {string} id id of the participant to kick
  373. */
  374. JitsiConference.prototype.muteParticipant = function (id) {
  375. var participant = this.getParticipantById(id);
  376. if (!participant) {
  377. return;
  378. }
  379. this.room.muteParticipant(participant.getJid(), true);
  380. };
  381. JitsiConference.prototype.onMemberJoined = function (jid, nick, role) {
  382. var id = Strophe.getResourceFromJid(jid);
  383. if (id === 'focus' || this.myUserId() === id) {
  384. return;
  385. }
  386. var participant = new JitsiParticipant(jid, this, nick);
  387. participant._role = role;
  388. this.participants[id] = participant;
  389. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id, participant);
  390. this.xmpp.connection.disco.info(
  391. jid, "node", function(iq) {
  392. participant._supportsDTMF = $(iq).find(
  393. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  394. this.updateDTMFSupport();
  395. }.bind(this)
  396. );
  397. };
  398. JitsiConference.prototype.onMemberLeft = function (jid) {
  399. var id = Strophe.getResourceFromJid(jid);
  400. if (id === 'focus' || this.myUserId() === id) {
  401. return;
  402. }
  403. var participant = this.participants[id];
  404. delete this.participants[id];
  405. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  406. };
  407. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  408. var id = Strophe.getResourceFromJid(jid);
  409. var participant = this.getParticipantById(id);
  410. if (!participant) {
  411. return;
  412. }
  413. participant._role = role;
  414. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  415. };
  416. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  417. var id = Strophe.getResourceFromJid(jid);
  418. var participant = this.getParticipantById(id);
  419. if (!participant) {
  420. return;
  421. }
  422. participant._displayName = displayName;
  423. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  424. };
  425. JitsiConference.prototype.onTrackAdded = function (track) {
  426. var id = track.getParticipantId();
  427. var participant = this.getParticipantById(id);
  428. if (!participant) {
  429. return;
  430. }
  431. // add track to JitsiParticipant
  432. participant._tracks.push(track);
  433. var emitter = this.eventEmitter;
  434. track.addEventListener(
  435. JitsiTrackEvents.TRACK_STOPPED,
  436. function () {
  437. // remove track from JitsiParticipant
  438. var pos = participant._tracks.indexOf(track);
  439. if (pos > -1) {
  440. participant._tracks.splice(pos, 1);
  441. }
  442. emitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  443. }
  444. );
  445. track.addEventListener(
  446. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  447. function () {
  448. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  449. }
  450. );
  451. track.addEventListener(
  452. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  453. function (audioLevel) {
  454. emitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  455. }
  456. );
  457. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  458. };
  459. JitsiConference.prototype.updateDTMFSupport = function () {
  460. var somebodySupportsDTMF = false;
  461. var participants = this.getParticipants();
  462. // check if at least 1 participant supports DTMF
  463. for (var i = 0; i < participants.length; i += 1) {
  464. if (participants[i].supportsDTMF()) {
  465. somebodySupportsDTMF = true;
  466. break;
  467. }
  468. }
  469. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  470. this.somebodySupportsDTMF = somebodySupportsDTMF;
  471. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  472. }
  473. };
  474. /**
  475. * Allows to check if there is at least one user in the conference
  476. * that supports DTMF.
  477. * @returns {boolean} true if somebody supports DTMF, false otherwise
  478. */
  479. JitsiConference.prototype.isDTMFSupported = function () {
  480. return this.somebodySupportsDTMF;
  481. };
  482. /**
  483. * Returns the local user's ID
  484. * @return {string} local user's ID
  485. */
  486. JitsiConference.prototype.myUserId = function () {
  487. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  488. };
  489. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  490. if (!this.dtmfManager) {
  491. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  492. if (!connection) {
  493. logger.warn("cannot sendTones: no conneciton");
  494. return;
  495. }
  496. var tracks = this.getLocalTracks().filter(function (track) {
  497. return track.isAudioTrack();
  498. });
  499. if (!tracks.length) {
  500. logger.warn("cannot sendTones: no local audio stream");
  501. return;
  502. }
  503. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  504. }
  505. this.dtmfManager.sendTones(tones, duration, pause);
  506. };
  507. /**
  508. * Returns true if the recording is supproted and false if not.
  509. */
  510. JitsiConference.prototype.isRecordingSupported = function () {
  511. if(this.room)
  512. return this.room.isRecordingSupported();
  513. return false;
  514. };
  515. /**
  516. * Returns null if the recording is not supported, "on" if the recording started
  517. * and "off" if the recording is not started.
  518. */
  519. JitsiConference.prototype.getRecordingState = function () {
  520. if(this.room)
  521. return this.room.getRecordingState();
  522. return "off";
  523. }
  524. /**
  525. * Returns the url of the recorded video.
  526. */
  527. JitsiConference.prototype.getRecordingURL = function () {
  528. if(this.room)
  529. return this.room.getRecordingURL();
  530. return null;
  531. }
  532. /**
  533. * Starts/stops the recording
  534. */
  535. JitsiConference.prototype.toggleRecording = function (options) {
  536. if(this.room)
  537. return this.room.toggleRecording(options, function (status, error) {
  538. this.eventEmitter.emit(
  539. JitsiConferenceEvents.RECORDING_STATE_CHANGED, status, error);
  540. }.bind(this));
  541. this.eventEmitter.emit(
  542. JitsiConferenceEvents.RECORDING_STATE_CHANGED, "error",
  543. new Error("The conference is not created yet!"));
  544. }
  545. /**
  546. * Returns true if the SIP calls are supported and false otherwise
  547. */
  548. JitsiConference.prototype.isSIPCallingSupported = function () {
  549. if(this.room)
  550. return this.room.isSIPCallingSupported();
  551. return false;
  552. }
  553. /**
  554. * Dials a number.
  555. * @param number the number
  556. */
  557. JitsiConference.prototype.dial = function (number) {
  558. if(this.room)
  559. return this.room.dial(number);
  560. return new Promise(function(resolve, reject){
  561. reject(new Error("The conference is not created yet!"))});
  562. }
  563. /**
  564. * Hangup an existing call
  565. */
  566. JitsiConference.prototype.hangup = function () {
  567. if(this.room)
  568. return this.room.hangup();
  569. return new Promise(function(resolve, reject){
  570. reject(new Error("The conference is not created yet!"))});
  571. }
  572. /**
  573. * Returns the phone number for joining the conference.
  574. */
  575. JitsiConference.prototype.getPhoneNumber = function () {
  576. if(this.room)
  577. return this.room.getPhoneNumber();
  578. return null;
  579. }
  580. /**
  581. * Returns the pin for joining the conference with phone.
  582. */
  583. JitsiConference.prototype.getPhonePin = function () {
  584. if(this.room)
  585. return this.room.getPhonePin();
  586. return null;
  587. }
  588. /**
  589. * Returns the connection state for the current room. Its ice connection state
  590. * for its session.
  591. */
  592. JitsiConference.prototype.getConnectionState = function () {
  593. if(this.room)
  594. return this.room.getConnectionState();
  595. return null;
  596. }
  597. /**
  598. * Make all new participants mute their audio/video on join.
  599. * @param policy {Object} object with 2 boolean properties for video and audio:
  600. * @param {boolean} audio if audio should be muted.
  601. * @param {boolean} video if video should be muted.
  602. */
  603. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  604. if (!this.isModerator()) {
  605. return;
  606. }
  607. this.startMutedPolicy = policy;
  608. this.room.removeFromPresence("startmuted");
  609. this.room.addToPresence("startmuted", {
  610. attributes: {
  611. audio: policy.audio,
  612. video: policy.video,
  613. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  614. }
  615. });
  616. this.room.sendPresence();
  617. };
  618. /**
  619. * Returns current start muted policy
  620. * @returns {Object} with 2 proprties - audio and video.
  621. */
  622. JitsiConference.prototype.getStartMutedPolicy = function () {
  623. return this.startMutedPolicy;
  624. };
  625. /**
  626. * Check if audio is muted on join.
  627. */
  628. JitsiConference.prototype.isStartAudioMuted = function () {
  629. return this.startAudioMuted;
  630. };
  631. /**
  632. * Check if video is muted on join.
  633. */
  634. JitsiConference.prototype.isStartVideoMuted = function () {
  635. return this.startVideoMuted;
  636. };
  637. /**
  638. * Setups the listeners needed for the conference.
  639. * @param conference the conference
  640. */
  641. function setupListeners(conference) {
  642. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  643. conference.rtc.onIncommingCall(event);
  644. if(conference.statistics)
  645. conference.statistics.startRemoteStats(event.peerconnection);
  646. });
  647. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  648. function (data, sid, thessrc) {
  649. var track = conference.rtc.createRemoteStream(data, sid, thessrc);
  650. if (track) {
  651. conference.onTrackAdded(track);
  652. }
  653. }
  654. );
  655. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  656. function (value) {
  657. conference.rtc.setAudioMute(value);
  658. }
  659. );
  660. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  661. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  662. });
  663. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  664. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  665. });
  666. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  667. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  668. });
  669. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  670. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  671. });
  672. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  673. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  674. });
  675. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  676. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  677. });
  678. // FIXME
  679. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  680. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  681. // });
  682. conference.room.addListener(XMPPEvents.KICKED, function () {
  683. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  684. });
  685. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  686. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  687. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  688. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  689. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  690. });
  691. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  692. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  693. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  694. });
  695. conference.room.addListener(XMPPEvents.RECORDING_STATE_CHANGED,
  696. function () {
  697. conference.eventEmitter.emit(
  698. JitsiConferenceEvents.RECORDING_STATE_CHANGED);
  699. });
  700. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  701. conference.eventEmitter.emit(
  702. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  703. });
  704. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  705. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  706. });
  707. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  708. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.SETUP_FAILED);
  709. });
  710. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  711. conference.authEnabled = authEnabled;
  712. conference.authIdentity = authIdentity;
  713. });
  714. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  715. var id = Strophe.getResourceFromJid(jid);
  716. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  717. });
  718. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  719. if(conference.lastDominantSpeaker !== id && conference.room) {
  720. conference.lastDominantSpeaker = id;
  721. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  722. }
  723. });
  724. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  725. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  726. });
  727. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  728. function (lastNEndpoints, endpointsEnteringLastN) {
  729. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  730. lastNEndpoints, endpointsEnteringLastN);
  731. });
  732. conference.xmpp.addListener(XMPPEvents.PASSWORD_REQUIRED, function () {
  733. conference.eventEmitter.emit(JitsiConferenceErrors.PASSWORD_REQUIRED);
  734. });
  735. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  736. function (audioMuted, videoMuted) {
  737. conference.startAudioMuted = audioMuted;
  738. conference.startVideoMuted = videoMuted;
  739. // mute existing local tracks because this is initial mute from
  740. // Jicofo
  741. conference.getLocalTracks().forEach(function (track) {
  742. if (conference.startAudioMuted && track.isAudioTrack()) {
  743. track.mute();
  744. }
  745. if (conference.startVideoMuted && track.isVideoTrack()) {
  746. track.mute();
  747. }
  748. });
  749. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  750. });
  751. conference.room.addPresenceListener("startmuted", function (data, from) {
  752. var isModerator = false;
  753. if (conference.myUserId() === from && conference.isModerator()) {
  754. isModerator = true;
  755. } else {
  756. var participant = conference.getParticipantById(from);
  757. if (participant && participant.isModerator()) {
  758. isModerator = true;
  759. }
  760. }
  761. if (!isModerator) {
  762. return;
  763. }
  764. var startAudioMuted = data.attributes.audio === 'true';
  765. var startVideoMuted = data.attributes.video === 'true';
  766. var updated = false;
  767. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  768. conference.startMutedPolicy.audio = startAudioMuted;
  769. updated = true;
  770. }
  771. if (startVideoMuted !== conference.startMutedPolicy.video) {
  772. conference.startMutedPolicy.video = startVideoMuted;
  773. updated = true;
  774. }
  775. if (updated) {
  776. conference.eventEmitter.emit(
  777. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  778. conference.startMutedPolicy
  779. );
  780. }
  781. });
  782. if(conference.statistics) {
  783. //FIXME: Maybe remove event should not be associated with the conference.
  784. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  785. var userId = null;
  786. var jid = conference.room.getJidBySSRC(ssrc);
  787. if (!jid)
  788. return;
  789. conference.rtc.setAudioLevel(jid, level);
  790. });
  791. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  792. function () {
  793. conference.statistics.dispose();
  794. });
  795. // FIXME: Maybe we should move this.
  796. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  797. // conference.room.updateDeviceAvailability(devices);
  798. // });
  799. }
  800. }
  801. module.exports = JitsiConference;