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

JitsiConference.js 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. track.muteHandler = this._fireMuteChangeEvent.bind(this, track);
  236. track.stopHandler = this.removeTrack.bind(this, track);
  237. track.audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  238. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, track.muteHandler);
  239. track.addEventListener(JitsiTrackEvents.TRACK_STOPPED, track.stopHandler);
  240. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, track.audioLevelHandler);
  241. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  242. }.bind(this));
  243. };
  244. /**
  245. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event.
  246. * @param audioLevel the audio level
  247. */
  248. JitsiConference.prototype._fireAudioLevelChangeEvent = function (audioLevel) {
  249. this.eventEmitter.emit(
  250. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  251. this.myUserId(), audioLevel);
  252. };
  253. /**
  254. * Fires TRACK_MUTE_CHANGED change conference event.
  255. * @param track the JitsiTrack object related to the event.
  256. */
  257. JitsiConference.prototype._fireMuteChangeEvent = function (track) {
  258. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  259. };
  260. /**
  261. * Removes JitsiLocalTrack object to the conference.
  262. * @param track the JitsiLocalTrack object.
  263. */
  264. JitsiConference.prototype.removeTrack = function (track) {
  265. if(!this.room){
  266. if(this.rtc)
  267. this.rtc.removeLocalStream(track);
  268. return;
  269. }
  270. this.room.removeStream(track.getOriginalStream(), function(){
  271. this.rtc.removeLocalStream(track);
  272. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, track.muteHandler);
  273. track.removeEventListener(JitsiTrackEvents.TRACK_STOPPED, track.stopHandler);
  274. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, track.audioLevelHandler);
  275. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  276. }.bind(this));
  277. };
  278. /**
  279. * Get role of the local user.
  280. * @returns {string} user role: 'moderator' or 'none'
  281. */
  282. JitsiConference.prototype.getRole = function () {
  283. return this.room.role;
  284. };
  285. /**
  286. * Check if local user is moderator.
  287. * @returns {boolean} true if local user is moderator, false otherwise.
  288. */
  289. JitsiConference.prototype.isModerator = function () {
  290. return this.room.isModerator();
  291. };
  292. /**
  293. * Set password for the room.
  294. * @param {string} password new password for the room.
  295. * @returns {Promise}
  296. */
  297. JitsiConference.prototype.lock = function (password) {
  298. if (!this.isModerator()) {
  299. return Promise.reject();
  300. }
  301. var conference = this;
  302. return new Promise(function (resolve, reject) {
  303. conference.room.lockRoom(password || "", function () {
  304. resolve();
  305. }, function (err) {
  306. reject(err);
  307. }, function () {
  308. reject(JitsiConferenceErrors.PASSWORD_NOT_SUPPORTED);
  309. });
  310. });
  311. };
  312. /**
  313. * Remove password from the room.
  314. * @returns {Promise}
  315. */
  316. JitsiConference.prototype.unlock = function () {
  317. return this.lock();
  318. };
  319. /**
  320. * Elects the participant with the given id to be the selected participant or the speaker.
  321. * @param id the identifier of the participant
  322. */
  323. JitsiConference.prototype.selectParticipant = function(participantId) {
  324. if (this.rtc) {
  325. this.rtc.selectedEndpoint(participantId);
  326. }
  327. };
  328. /**
  329. *
  330. * @param id the identifier of the participant
  331. */
  332. JitsiConference.prototype.pinParticipant = function(participantId) {
  333. if (this.rtc) {
  334. this.rtc.pinEndpoint(participantId);
  335. }
  336. };
  337. /**
  338. * Returns the list of participants for this conference.
  339. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  340. */
  341. JitsiConference.prototype.getParticipants = function() {
  342. return Object.keys(this.participants).map(function (key) {
  343. return this.participants[key];
  344. }, this);
  345. };
  346. /**
  347. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  348. * undefined if there isn't one).
  349. * @param id the id of the participant.
  350. */
  351. JitsiConference.prototype.getParticipantById = function(id) {
  352. return this.participants[id];
  353. };
  354. /**
  355. * Kick participant from this conference.
  356. * @param {string} id id of the participant to kick
  357. */
  358. JitsiConference.prototype.kickParticipant = function (id) {
  359. var participant = this.getParticipantById(id);
  360. if (!participant) {
  361. return;
  362. }
  363. this.room.kick(participant.getJid());
  364. };
  365. /**
  366. * Kick participant from this conference.
  367. * @param {string} id id of the participant to kick
  368. */
  369. JitsiConference.prototype.muteParticipant = function (id) {
  370. var participant = this.getParticipantById(id);
  371. if (!participant) {
  372. return;
  373. }
  374. this.room.muteParticipant(participant.getJid(), true);
  375. };
  376. JitsiConference.prototype.onMemberJoined = function (jid, nick, role) {
  377. var id = Strophe.getResourceFromJid(jid);
  378. if (id === 'focus' || this.myUserId() === id) {
  379. return;
  380. }
  381. var participant = new JitsiParticipant(jid, this, nick);
  382. participant._role = role;
  383. this.participants[id] = participant;
  384. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id, participant);
  385. this.xmpp.connection.disco.info(
  386. jid, "node", function(iq) {
  387. participant._supportsDTMF = $(iq).find(
  388. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  389. this.updateDTMFSupport();
  390. }.bind(this)
  391. );
  392. };
  393. JitsiConference.prototype.onMemberLeft = function (jid) {
  394. var id = Strophe.getResourceFromJid(jid);
  395. if (id === 'focus' || this.myUserId() === id) {
  396. return;
  397. }
  398. var participant = this.participants[id];
  399. delete this.participants[id];
  400. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  401. };
  402. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  403. var id = Strophe.getResourceFromJid(jid);
  404. var participant = this.getParticipantById(id);
  405. if (!participant) {
  406. return;
  407. }
  408. participant._role = role;
  409. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  410. };
  411. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  412. var id = Strophe.getResourceFromJid(jid);
  413. var participant = this.getParticipantById(id);
  414. if (!participant) {
  415. return;
  416. }
  417. participant._displayName = displayName;
  418. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  419. };
  420. JitsiConference.prototype.onTrackAdded = function (track) {
  421. var id = track.getParticipantId();
  422. var participant = this.getParticipantById(id);
  423. if (!participant) {
  424. return;
  425. }
  426. // add track to JitsiParticipant
  427. participant._tracks.push(track);
  428. var emitter = this.eventEmitter;
  429. track.addEventListener(
  430. JitsiTrackEvents.TRACK_STOPPED,
  431. function () {
  432. // remove track from JitsiParticipant
  433. var pos = participant._tracks.indexOf(track);
  434. if (pos > -1) {
  435. participant._tracks.splice(pos, 1);
  436. }
  437. emitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  438. }
  439. );
  440. track.addEventListener(
  441. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  442. function () {
  443. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  444. }
  445. );
  446. track.addEventListener(
  447. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  448. function (audioLevel) {
  449. emitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  450. }
  451. );
  452. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  453. };
  454. JitsiConference.prototype.updateDTMFSupport = function () {
  455. var somebodySupportsDTMF = false;
  456. var participants = this.getParticipants();
  457. // check if at least 1 participant supports DTMF
  458. for (var i = 0; i < participants.length; i += 1) {
  459. if (participants[i].supportsDTMF()) {
  460. somebodySupportsDTMF = true;
  461. break;
  462. }
  463. }
  464. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  465. this.somebodySupportsDTMF = somebodySupportsDTMF;
  466. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  467. }
  468. };
  469. /**
  470. * Allows to check if there is at least one user in the conference
  471. * that supports DTMF.
  472. * @returns {boolean} true if somebody supports DTMF, false otherwise
  473. */
  474. JitsiConference.prototype.isDTMFSupported = function () {
  475. return this.somebodySupportsDTMF;
  476. };
  477. /**
  478. * Returns the local user's ID
  479. * @return {string} local user's ID
  480. */
  481. JitsiConference.prototype.myUserId = function () {
  482. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  483. };
  484. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  485. if (!this.dtmfManager) {
  486. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  487. if (!connection) {
  488. logger.warn("cannot sendTones: no conneciton");
  489. return;
  490. }
  491. var tracks = this.getLocalTracks().filter(function (track) {
  492. return track.isAudioTrack();
  493. });
  494. if (!tracks.length) {
  495. logger.warn("cannot sendTones: no local audio stream");
  496. return;
  497. }
  498. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  499. }
  500. this.dtmfManager.sendTones(tones, duration, pause);
  501. };
  502. /**
  503. * Returns true if the recording is supproted and false if not.
  504. */
  505. JitsiConference.prototype.isRecordingSupported = function () {
  506. if(this.room)
  507. return this.room.isRecordingSupported();
  508. return false;
  509. };
  510. /**
  511. * Returns null if the recording is not supported, "on" if the recording started
  512. * and "off" if the recording is not started.
  513. */
  514. JitsiConference.prototype.getRecordingState = function () {
  515. if(this.room)
  516. return this.room.getRecordingState();
  517. return "off";
  518. }
  519. /**
  520. * Returns the url of the recorded video.
  521. */
  522. JitsiConference.prototype.getRecordingURL = function () {
  523. if(this.room)
  524. return this.room.getRecordingURL();
  525. return null;
  526. }
  527. /**
  528. * Starts/stops the recording
  529. */
  530. JitsiConference.prototype.toggleRecording = function (options) {
  531. if(this.room)
  532. return this.room.toggleRecording(options, function (status, error) {
  533. this.eventEmitter.emit(
  534. JitsiConferenceEvents.RECORDING_STATE_CHANGED, status, error);
  535. }.bind(this));
  536. this.eventEmitter.emit(
  537. JitsiConferenceEvents.RECORDING_STATE_CHANGED, "error",
  538. new Error("The conference is not created yet!"));
  539. }
  540. /**
  541. * Returns true if the SIP calls are supported and false otherwise
  542. */
  543. JitsiConference.prototype.isSIPCallingSupported = function () {
  544. if(this.room)
  545. return this.room.isSIPCallingSupported();
  546. return false;
  547. }
  548. /**
  549. * Dials a number.
  550. * @param number the number
  551. */
  552. JitsiConference.prototype.dial = function (number) {
  553. if(this.room)
  554. return this.room.dial(number);
  555. return new Promise(function(resolve, reject){
  556. reject(new Error("The conference is not created yet!"))});
  557. }
  558. /**
  559. * Hangup an existing call
  560. */
  561. JitsiConference.prototype.hangup = function () {
  562. if(this.room)
  563. return this.room.hangup();
  564. return new Promise(function(resolve, reject){
  565. reject(new Error("The conference is not created yet!"))});
  566. }
  567. /**
  568. * Returns the phone number for joining the conference.
  569. */
  570. JitsiConference.prototype.getPhoneNumber = function () {
  571. if(this.room)
  572. return this.room.getPhoneNumber();
  573. return null;
  574. }
  575. /**
  576. * Returns the pin for joining the conference with phone.
  577. */
  578. JitsiConference.prototype.getPhonePin = function () {
  579. if(this.room)
  580. return this.room.getPhonePin();
  581. return null;
  582. }
  583. /**
  584. * Returns the connection state for the current room. Its ice connection state
  585. * for its session.
  586. */
  587. JitsiConference.prototype.getConnectionState = function () {
  588. if(this.room)
  589. return this.room.getConnectionState();
  590. return null;
  591. }
  592. /**
  593. * Make all new participants mute their audio/video on join.
  594. * @param policy {Object} object with 2 boolean properties for video and audio:
  595. * @param {boolean} audio if audio should be muted.
  596. * @param {boolean} video if video should be muted.
  597. */
  598. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  599. if (!this.isModerator()) {
  600. return;
  601. }
  602. this.startMutedPolicy = policy;
  603. this.room.removeFromPresence("startmuted");
  604. this.room.addToPresence("startmuted", {
  605. attributes: {
  606. audio: policy.audio,
  607. video: policy.video,
  608. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  609. }
  610. });
  611. this.room.sendPresence();
  612. };
  613. /**
  614. * Returns current start muted policy
  615. * @returns {Object} with 2 proprties - audio and video.
  616. */
  617. JitsiConference.prototype.getStartMutedPolicy = function () {
  618. return this.startMutedPolicy;
  619. };
  620. /**
  621. * Check if audio is muted on join.
  622. */
  623. JitsiConference.prototype.isStartAudioMuted = function () {
  624. return this.startAudioMuted;
  625. };
  626. /**
  627. * Check if video is muted on join.
  628. */
  629. JitsiConference.prototype.isStartVideoMuted = function () {
  630. return this.startVideoMuted;
  631. };
  632. /**
  633. * Get object with internal logs.
  634. */
  635. JitsiConference.prototype.getLogs = function () {
  636. var data = this.xmpp.getJingleLog();
  637. var metadata = {};
  638. metadata.time = new Date();
  639. metadata.url = window.location.href;
  640. metadata.ua = navigator.userAgent;
  641. var log = this.xmpp.getXmppLog();
  642. if (log) {
  643. metadata.xmpp = log;
  644. }
  645. data.metadata = metadata;
  646. return data;
  647. };
  648. /**
  649. * Setups the listeners needed for the conference.
  650. * @param conference the conference
  651. */
  652. function setupListeners(conference) {
  653. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  654. conference.rtc.onIncommingCall(event);
  655. if(conference.statistics)
  656. conference.statistics.startRemoteStats(event.peerconnection);
  657. });
  658. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  659. function (data, sid, thessrc) {
  660. var track = conference.rtc.createRemoteStream(data, sid, thessrc);
  661. if (track) {
  662. conference.onTrackAdded(track);
  663. }
  664. }
  665. );
  666. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  667. function (value) {
  668. conference.rtc.setAudioMute(value);
  669. }
  670. );
  671. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  672. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  673. });
  674. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  675. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  676. });
  677. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  678. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  679. });
  680. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  681. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  682. });
  683. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  684. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  685. });
  686. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  687. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  688. });
  689. // FIXME
  690. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  691. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  692. // });
  693. conference.room.addListener(XMPPEvents.KICKED, function () {
  694. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  695. });
  696. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  697. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  698. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  699. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  700. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  701. });
  702. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  703. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  704. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  705. });
  706. conference.room.addListener(XMPPEvents.RECORDING_STATE_CHANGED,
  707. function () {
  708. conference.eventEmitter.emit(
  709. JitsiConferenceEvents.RECORDING_STATE_CHANGED);
  710. });
  711. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  712. conference.eventEmitter.emit(
  713. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  714. });
  715. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  716. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  717. });
  718. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  719. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.SETUP_FAILED);
  720. });
  721. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  722. conference.authEnabled = authEnabled;
  723. conference.authIdentity = authIdentity;
  724. });
  725. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  726. var id = Strophe.getResourceFromJid(jid);
  727. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  728. });
  729. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  730. if(conference.lastDominantSpeaker !== id && conference.room) {
  731. conference.lastDominantSpeaker = id;
  732. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  733. }
  734. });
  735. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  736. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  737. });
  738. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  739. function (lastNEndpoints, endpointsEnteringLastN) {
  740. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  741. lastNEndpoints, endpointsEnteringLastN);
  742. });
  743. conference.xmpp.addListener(XMPPEvents.PASSWORD_REQUIRED, function () {
  744. conference.eventEmitter.emit(JitsiConferenceErrors.PASSWORD_REQUIRED);
  745. });
  746. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  747. function (audioMuted, videoMuted) {
  748. conference.startAudioMuted = audioMuted;
  749. conference.startVideoMuted = videoMuted;
  750. // mute existing local tracks because this is initial mute from
  751. // Jicofo
  752. conference.getLocalTracks().forEach(function (track) {
  753. if (conference.startAudioMuted && track.isAudioTrack()) {
  754. track.mute();
  755. }
  756. if (conference.startVideoMuted && track.isVideoTrack()) {
  757. track.mute();
  758. }
  759. });
  760. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  761. });
  762. conference.room.addPresenceListener("startmuted", function (data, from) {
  763. var isModerator = false;
  764. if (conference.myUserId() === from && conference.isModerator()) {
  765. isModerator = true;
  766. } else {
  767. var participant = conference.getParticipantById(from);
  768. if (participant && participant.isModerator()) {
  769. isModerator = true;
  770. }
  771. }
  772. if (!isModerator) {
  773. return;
  774. }
  775. var startAudioMuted = data.attributes.audio === 'true';
  776. var startVideoMuted = data.attributes.video === 'true';
  777. var updated = false;
  778. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  779. conference.startMutedPolicy.audio = startAudioMuted;
  780. updated = true;
  781. }
  782. if (startVideoMuted !== conference.startMutedPolicy.video) {
  783. conference.startMutedPolicy.video = startVideoMuted;
  784. updated = true;
  785. }
  786. if (updated) {
  787. conference.eventEmitter.emit(
  788. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  789. conference.startMutedPolicy
  790. );
  791. }
  792. });
  793. if(conference.statistics) {
  794. //FIXME: Maybe remove event should not be associated with the conference.
  795. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  796. var userId = null;
  797. var jid = conference.room.getJidBySSRC(ssrc);
  798. if (!jid)
  799. return;
  800. conference.rtc.setAudioLevel(jid, level);
  801. });
  802. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  803. function () {
  804. conference.statistics.dispose();
  805. });
  806. // FIXME: Maybe we should move this.
  807. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  808. // conference.room.updateDeviceAvailability(devices);
  809. // });
  810. }
  811. }
  812. module.exports = JitsiConference;