Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JitsiConference.js 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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. return Promise.resolve();
  316. }
  317. return new Promise(function (resolve) {
  318. this.room.removeStream(track.getOriginalStream(), function(){
  319. this.rtc.removeLocalStream(track);
  320. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED, track.muteHandler);
  321. track.removeEventListener(JitsiTrackEvents.TRACK_STOPPED, track.stopHandler);
  322. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED, track.audioLevelHandler);
  323. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  324. resolve();
  325. }.bind(this));
  326. }.bind(this));
  327. };
  328. /**
  329. * Get role of the local user.
  330. * @returns {string} user role: 'moderator' or 'none'
  331. */
  332. JitsiConference.prototype.getRole = function () {
  333. return this.room.role;
  334. };
  335. /**
  336. * Check if local user is moderator.
  337. * @returns {boolean} true if local user is moderator, false otherwise.
  338. */
  339. JitsiConference.prototype.isModerator = function () {
  340. return this.room.isModerator();
  341. };
  342. /**
  343. * Set password for the room.
  344. * @param {string} password new password for the room.
  345. * @returns {Promise}
  346. */
  347. JitsiConference.prototype.lock = function (password) {
  348. if (!this.isModerator()) {
  349. return Promise.reject();
  350. }
  351. var conference = this;
  352. return new Promise(function (resolve, reject) {
  353. conference.room.lockRoom(password || "", function () {
  354. resolve();
  355. }, function (err) {
  356. reject(err);
  357. }, function () {
  358. reject(JitsiConferenceErrors.PASSWORD_NOT_SUPPORTED);
  359. });
  360. });
  361. };
  362. /**
  363. * Remove password from the room.
  364. * @returns {Promise}
  365. */
  366. JitsiConference.prototype.unlock = function () {
  367. return this.lock();
  368. };
  369. /**
  370. * Elects the participant with the given id to be the selected participant or the speaker.
  371. * @param id the identifier of the participant
  372. */
  373. JitsiConference.prototype.selectParticipant = function(participantId) {
  374. if (this.rtc) {
  375. this.rtc.selectedEndpoint(participantId);
  376. }
  377. };
  378. /**
  379. *
  380. * @param id the identifier of the participant
  381. */
  382. JitsiConference.prototype.pinParticipant = function(participantId) {
  383. if (this.rtc) {
  384. this.rtc.pinEndpoint(participantId);
  385. }
  386. };
  387. /**
  388. * Returns the list of participants for this conference.
  389. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  390. */
  391. JitsiConference.prototype.getParticipants = function() {
  392. return Object.keys(this.participants).map(function (key) {
  393. return this.participants[key];
  394. }, this);
  395. };
  396. /**
  397. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  398. * undefined if there isn't one).
  399. * @param id the id of the participant.
  400. */
  401. JitsiConference.prototype.getParticipantById = function(id) {
  402. return this.participants[id];
  403. };
  404. /**
  405. * Kick participant from this conference.
  406. * @param {string} id id of the participant to kick
  407. */
  408. JitsiConference.prototype.kickParticipant = function (id) {
  409. var participant = this.getParticipantById(id);
  410. if (!participant) {
  411. return;
  412. }
  413. this.room.kick(participant.getJid());
  414. };
  415. /**
  416. * Kick participant from this conference.
  417. * @param {string} id id of the participant to kick
  418. */
  419. JitsiConference.prototype.muteParticipant = function (id) {
  420. var participant = this.getParticipantById(id);
  421. if (!participant) {
  422. return;
  423. }
  424. this.room.muteParticipant(participant.getJid(), true);
  425. };
  426. JitsiConference.prototype.onMemberJoined = function (jid, nick, role) {
  427. var id = Strophe.getResourceFromJid(jid);
  428. if (id === 'focus' || this.myUserId() === id) {
  429. return;
  430. }
  431. var participant = new JitsiParticipant(jid, this, nick);
  432. participant._role = role;
  433. this.participants[id] = participant;
  434. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id, participant);
  435. this.xmpp.connection.disco.info(
  436. jid, "node", function(iq) {
  437. participant._supportsDTMF = $(iq).find(
  438. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  439. this.updateDTMFSupport();
  440. }.bind(this)
  441. );
  442. };
  443. JitsiConference.prototype.onMemberLeft = function (jid) {
  444. var id = Strophe.getResourceFromJid(jid);
  445. if (id === 'focus' || this.myUserId() === id) {
  446. return;
  447. }
  448. var participant = this.participants[id];
  449. delete this.participants[id];
  450. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  451. };
  452. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  453. var id = Strophe.getResourceFromJid(jid);
  454. var participant = this.getParticipantById(id);
  455. if (!participant) {
  456. return;
  457. }
  458. participant._role = role;
  459. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  460. };
  461. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  462. var id = Strophe.getResourceFromJid(jid);
  463. var participant = this.getParticipantById(id);
  464. if (!participant) {
  465. return;
  466. }
  467. participant._displayName = displayName;
  468. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  469. };
  470. JitsiConference.prototype.onTrackAdded = function (track) {
  471. var id = track.getParticipantId();
  472. var participant = this.getParticipantById(id);
  473. if (!participant) {
  474. return;
  475. }
  476. // add track to JitsiParticipant
  477. participant._tracks.push(track);
  478. var emitter = this.eventEmitter;
  479. track.addEventListener(
  480. JitsiTrackEvents.TRACK_STOPPED,
  481. function () {
  482. // remove track from JitsiParticipant
  483. var pos = participant._tracks.indexOf(track);
  484. if (pos > -1) {
  485. participant._tracks.splice(pos, 1);
  486. }
  487. emitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  488. }
  489. );
  490. track.addEventListener(
  491. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  492. function () {
  493. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  494. }
  495. );
  496. track.addEventListener(
  497. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  498. function (audioLevel) {
  499. emitter.emit(JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED, id, audioLevel);
  500. }
  501. );
  502. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  503. };
  504. JitsiConference.prototype.updateDTMFSupport = function () {
  505. var somebodySupportsDTMF = false;
  506. var participants = this.getParticipants();
  507. // check if at least 1 participant supports DTMF
  508. for (var i = 0; i < participants.length; i += 1) {
  509. if (participants[i].supportsDTMF()) {
  510. somebodySupportsDTMF = true;
  511. break;
  512. }
  513. }
  514. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  515. this.somebodySupportsDTMF = somebodySupportsDTMF;
  516. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  517. }
  518. };
  519. /**
  520. * Allows to check if there is at least one user in the conference
  521. * that supports DTMF.
  522. * @returns {boolean} true if somebody supports DTMF, false otherwise
  523. */
  524. JitsiConference.prototype.isDTMFSupported = function () {
  525. return this.somebodySupportsDTMF;
  526. };
  527. /**
  528. * Returns the local user's ID
  529. * @return {string} local user's ID
  530. */
  531. JitsiConference.prototype.myUserId = function () {
  532. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  533. };
  534. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  535. if (!this.dtmfManager) {
  536. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  537. if (!connection) {
  538. logger.warn("cannot sendTones: no conneciton");
  539. return;
  540. }
  541. var tracks = this.getLocalTracks().filter(function (track) {
  542. return track.isAudioTrack();
  543. });
  544. if (!tracks.length) {
  545. logger.warn("cannot sendTones: no local audio stream");
  546. return;
  547. }
  548. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  549. }
  550. this.dtmfManager.sendTones(tones, duration, pause);
  551. };
  552. /**
  553. * Returns true if the recording is supproted and false if not.
  554. */
  555. JitsiConference.prototype.isRecordingSupported = function () {
  556. if(this.room)
  557. return this.room.isRecordingSupported();
  558. return false;
  559. };
  560. /**
  561. * Returns null if the recording is not supported, "on" if the recording started
  562. * and "off" if the recording is not started.
  563. */
  564. JitsiConference.prototype.getRecordingState = function () {
  565. if(this.room)
  566. return this.room.getRecordingState();
  567. return "off";
  568. }
  569. /**
  570. * Returns the url of the recorded video.
  571. */
  572. JitsiConference.prototype.getRecordingURL = function () {
  573. if(this.room)
  574. return this.room.getRecordingURL();
  575. return null;
  576. }
  577. /**
  578. * Starts/stops the recording
  579. */
  580. JitsiConference.prototype.toggleRecording = function (options) {
  581. if(this.room)
  582. return this.room.toggleRecording(options, function (status, error) {
  583. this.eventEmitter.emit(
  584. JitsiConferenceEvents.RECORDING_STATE_CHANGED, status, error);
  585. }.bind(this));
  586. this.eventEmitter.emit(
  587. JitsiConferenceEvents.RECORDING_STATE_CHANGED, "error",
  588. new Error("The conference is not created yet!"));
  589. }
  590. /**
  591. * Returns true if the SIP calls are supported and false otherwise
  592. */
  593. JitsiConference.prototype.isSIPCallingSupported = function () {
  594. if(this.room)
  595. return this.room.isSIPCallingSupported();
  596. return false;
  597. }
  598. /**
  599. * Dials a number.
  600. * @param number the number
  601. */
  602. JitsiConference.prototype.dial = function (number) {
  603. if(this.room)
  604. return this.room.dial(number);
  605. return new Promise(function(resolve, reject){
  606. reject(new Error("The conference is not created yet!"))});
  607. }
  608. /**
  609. * Hangup an existing call
  610. */
  611. JitsiConference.prototype.hangup = function () {
  612. if(this.room)
  613. return this.room.hangup();
  614. return new Promise(function(resolve, reject){
  615. reject(new Error("The conference is not created yet!"))});
  616. }
  617. /**
  618. * Returns the phone number for joining the conference.
  619. */
  620. JitsiConference.prototype.getPhoneNumber = function () {
  621. if(this.room)
  622. return this.room.getPhoneNumber();
  623. return null;
  624. }
  625. /**
  626. * Returns the pin for joining the conference with phone.
  627. */
  628. JitsiConference.prototype.getPhonePin = function () {
  629. if(this.room)
  630. return this.room.getPhonePin();
  631. return null;
  632. }
  633. /**
  634. * Returns the connection state for the current room. Its ice connection state
  635. * for its session.
  636. */
  637. JitsiConference.prototype.getConnectionState = function () {
  638. if(this.room)
  639. return this.room.getConnectionState();
  640. return null;
  641. }
  642. /**
  643. * Make all new participants mute their audio/video on join.
  644. * @param policy {Object} object with 2 boolean properties for video and audio:
  645. * @param {boolean} audio if audio should be muted.
  646. * @param {boolean} video if video should be muted.
  647. */
  648. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  649. if (!this.isModerator()) {
  650. return;
  651. }
  652. this.startMutedPolicy = policy;
  653. this.room.removeFromPresence("startmuted");
  654. this.room.addToPresence("startmuted", {
  655. attributes: {
  656. audio: policy.audio,
  657. video: policy.video,
  658. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  659. }
  660. });
  661. this.room.sendPresence();
  662. };
  663. /**
  664. * Returns current start muted policy
  665. * @returns {Object} with 2 proprties - audio and video.
  666. */
  667. JitsiConference.prototype.getStartMutedPolicy = function () {
  668. return this.startMutedPolicy;
  669. };
  670. /**
  671. * Check if audio is muted on join.
  672. */
  673. JitsiConference.prototype.isStartAudioMuted = function () {
  674. return this.startAudioMuted;
  675. };
  676. /**
  677. * Check if video is muted on join.
  678. */
  679. JitsiConference.prototype.isStartVideoMuted = function () {
  680. return this.startVideoMuted;
  681. };
  682. /**
  683. * Get object with internal logs.
  684. */
  685. JitsiConference.prototype.getLogs = function () {
  686. var data = this.xmpp.getJingleLog();
  687. var metadata = {};
  688. metadata.time = new Date();
  689. metadata.url = window.location.href;
  690. metadata.ua = navigator.userAgent;
  691. var log = this.xmpp.getXmppLog();
  692. if (log) {
  693. metadata.xmpp = log;
  694. }
  695. data.metadata = metadata;
  696. return data;
  697. };
  698. /**
  699. * Sends the given feedback through CallStats if enabled.
  700. *
  701. * @param overallFeedback an integer between 1 and 5 indicating the
  702. * user feedback
  703. * @param detailedFeedback detailed feedback from the user. Not yet used
  704. */
  705. JitsiConference.prototype.sendFeedback =
  706. function(overallFeedback, detailedFeedback){
  707. this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  708. }
  709. /**
  710. * Returns true if the callstats integration is enabled, otherwise returns
  711. * false.
  712. *
  713. * @returns true if the callstats integration is enabled, otherwise returns
  714. * false.
  715. */
  716. JitsiConference.prototype.isCallstatsEnabled = function () {
  717. return this.statistics.isCallstatsEnabled();
  718. }
  719. /**
  720. * Setups the listeners needed for the conference.
  721. * @param conference the conference
  722. */
  723. function setupListeners(conference) {
  724. conference.xmpp.addListener(XMPPEvents.CALL_INCOMING, function (event) {
  725. conference.rtc.onIncommingCall(event);
  726. conference.statistics.startRemoteStats(event.peerconnection);
  727. });
  728. conference.room.addListener(XMPPEvents.REMOTE_STREAM_RECEIVED,
  729. function (data, sid, thessrc) {
  730. var track = conference.rtc.createRemoteStream(data, sid, thessrc);
  731. if (track) {
  732. conference.onTrackAdded(track);
  733. }
  734. }
  735. );
  736. conference.rtc.addListener(RTCEvents.FAKE_VIDEO_TRACK_CREATED,
  737. function (track) {
  738. conference.onTrackAdded(track);
  739. }
  740. );
  741. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  742. function (value) {
  743. conference.rtc.setAudioMute(value);
  744. conference.isMutedByFocus = true;
  745. }
  746. );
  747. conference.room.addListener(XMPPEvents.SUBJECT_CHANGED, function (subject) {
  748. conference.eventEmitter.emit(JitsiConferenceEvents.SUBJECT_CHANGED, subject);
  749. });
  750. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  751. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  752. });
  753. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  754. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  755. });
  756. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  757. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONNECTION_ERROR, pres);
  758. });
  759. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  760. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  761. });
  762. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  763. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  764. });
  765. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  766. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  767. });
  768. conference.room.addListener(XMPPEvents.RESERVATION_ERROR, function (code, msg) {
  769. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.RESERVATION_ERROR, code, msg);
  770. });
  771. conference.room.addListener(XMPPEvents.GRACEFUL_SHUTDOWN, function () {
  772. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  773. });
  774. conference.room.addListener(XMPPEvents.JINGLE_FATAL_ERROR, function () {
  775. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.JINGLE_FATAL_ERROR);
  776. });
  777. conference.room.addListener(XMPPEvents.MUC_DESTROYED, function (reason) {
  778. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONFERENCE_DESTROYED, reason);
  779. });
  780. conference.room.addListener(XMPPEvents.CHAT_ERROR_RECEIVED, function (err, msg) {
  781. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_ERROR, JitsiConferenceErrors.CHAT_ERROR, err, msg);
  782. });
  783. conference.room.addListener(XMPPEvents.FOCUS_DISCONNECTED, function (focus, retrySec) {
  784. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_DISCONNECTED, focus, retrySec);
  785. });
  786. // FIXME
  787. // conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  788. // conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  789. // });
  790. conference.room.addListener(XMPPEvents.KICKED, function () {
  791. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  792. });
  793. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  794. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  795. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  796. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  797. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  798. });
  799. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  800. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  801. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  802. });
  803. conference.room.addListener(XMPPEvents.RECORDING_STATE_CHANGED,
  804. function () {
  805. conference.eventEmitter.emit(
  806. JitsiConferenceEvents.RECORDING_STATE_CHANGED);
  807. });
  808. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  809. conference.eventEmitter.emit(
  810. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  811. });
  812. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  813. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  814. });
  815. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED, function () {
  816. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.SETUP_FAILED);
  817. });
  818. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  819. conference.authEnabled = authEnabled;
  820. conference.authIdentity = authIdentity;
  821. });
  822. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  823. var id = Strophe.getResourceFromJid(jid);
  824. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  825. });
  826. conference.room.addListener(XMPPEvents.PRESENCE_STATUS, function (jid, status) {
  827. var id = Strophe.getResourceFromJid(jid);
  828. var participant = conference.getParticipantById(id);
  829. if (!participant || participant._status === status) {
  830. return;
  831. }
  832. participant._status = status;
  833. conference.eventEmitter.emit(JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  834. });
  835. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  836. if(conference.lastDominantSpeaker !== id && conference.room) {
  837. conference.lastDominantSpeaker = id;
  838. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  839. }
  840. });
  841. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  842. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  843. });
  844. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  845. function (lastNEndpoints, endpointsEnteringLastN) {
  846. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  847. lastNEndpoints, endpointsEnteringLastN);
  848. });
  849. conference.xmpp.addListener(XMPPEvents.PASSWORD_REQUIRED, function () {
  850. conference.eventEmitter.emit(JitsiConferenceErrors.PASSWORD_REQUIRED);
  851. });
  852. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  853. function (audioMuted, videoMuted) {
  854. conference.startAudioMuted = audioMuted;
  855. conference.startVideoMuted = videoMuted;
  856. // mute existing local tracks because this is initial mute from
  857. // Jicofo
  858. conference.getLocalTracks().forEach(function (track) {
  859. if (conference.startAudioMuted && track.isAudioTrack()) {
  860. track.mute();
  861. }
  862. if (conference.startVideoMuted && track.isVideoTrack()) {
  863. track.mute();
  864. }
  865. });
  866. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  867. });
  868. conference.room.addPresenceListener("startmuted", function (data, from) {
  869. var isModerator = false;
  870. if (conference.myUserId() === from && conference.isModerator()) {
  871. isModerator = true;
  872. } else {
  873. var participant = conference.getParticipantById(from);
  874. if (participant && participant.isModerator()) {
  875. isModerator = true;
  876. }
  877. }
  878. if (!isModerator) {
  879. return;
  880. }
  881. var startAudioMuted = data.attributes.audio === 'true';
  882. var startVideoMuted = data.attributes.video === 'true';
  883. var updated = false;
  884. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  885. conference.startMutedPolicy.audio = startAudioMuted;
  886. updated = true;
  887. }
  888. if (startVideoMuted !== conference.startMutedPolicy.video) {
  889. conference.startMutedPolicy.video = startVideoMuted;
  890. updated = true;
  891. }
  892. if (updated) {
  893. conference.eventEmitter.emit(
  894. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  895. conference.startMutedPolicy
  896. );
  897. }
  898. });
  899. conference.rtc.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  900. conference.room.updateDeviceAvailability(devices);
  901. });
  902. conference.room.addPresenceListener("devices", function (data, from) {
  903. var isAudioAvailable = false;
  904. var isVideoAvailable = false;
  905. data.children.forEach(function (config) {
  906. if (config.tagName === 'audio') {
  907. isAudioAvailable = config.value === 'true';
  908. }
  909. if (config.tagName === 'video') {
  910. isVideoAvailable = config.value === 'true';
  911. }
  912. });
  913. var availableDevices;
  914. if (conference.myUserId() === from) {
  915. availableDevices = conference.availableDevices;
  916. } else {
  917. var participant = conference.getParticipantById(from);
  918. if (!participant) {
  919. return;
  920. }
  921. availableDevices = participant._availableDevices;
  922. }
  923. var updated = false;
  924. if (availableDevices.audio !== isAudioAvailable) {
  925. updated = true;
  926. availableDevices.audio = isAudioAvailable;
  927. }
  928. if (availableDevices.video !== isVideoAvailable) {
  929. updated = true;
  930. availableDevices.video = isVideoAvailable;
  931. }
  932. if (updated) {
  933. conference.eventEmitter.emit(JitsiConferenceEvents.AVAILABLE_DEVICES_CHANGED, from, availableDevices);
  934. }
  935. });
  936. if(conference.statistics) {
  937. //FIXME: Maybe remove event should not be associated with the conference.
  938. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  939. var userId = null;
  940. var jid = conference.room.getJidBySSRC(ssrc);
  941. if (!jid)
  942. return;
  943. conference.rtc.setAudioLevel(jid, level);
  944. });
  945. conference.statistics.addConnectionStatsListener(function (stats) {
  946. var ssrc2resolution = stats.resolution;
  947. var id2resolution = {};
  948. // preprocess resolutions: group by user id, skip incorrect
  949. // resolutions etc.
  950. Object.keys(ssrc2resolution).forEach(function (ssrc) {
  951. var resolution = ssrc2resolution[ssrc];
  952. if (!resolution.width || !resolution.height ||
  953. resolution.width == -1 || resolution.height == -1) {
  954. return;
  955. }
  956. var jid = conference.room.getJidBySSRC(ssrc);
  957. if (!jid) {
  958. return;
  959. }
  960. var id;
  961. if (jid === conference.room.session.me) {
  962. id = conference.myUserId();
  963. } else {
  964. id = Strophe.getResourceFromJid(jid);
  965. }
  966. if (!id) {
  967. return;
  968. }
  969. // ssrc to resolution map for user id
  970. var idResolutions = id2resolution[id] || {};
  971. idResolutions[ssrc] = resolution;
  972. id2resolution[id] = idResolutions;
  973. });
  974. stats.resolution = id2resolution;
  975. conference.eventEmitter.emit(
  976. JitsiConferenceEvents.CONNECTION_STATS, stats);
  977. });
  978. conference.xmpp.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  979. function () {
  980. conference.statistics.dispose();
  981. });
  982. conference.room.addListener(XMPPEvents.PEERCONNECTION_READY,
  983. function (session) {
  984. conference.statistics.startCallStats(
  985. session, conference.settings);
  986. });
  987. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED,
  988. function () {
  989. conference.statistics.sendSetupFailedEvent();
  990. });
  991. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  992. function (track) {
  993. if(!track.isLocal())
  994. return;
  995. var type = (track.getType() === "audio")? "audio" : "video";
  996. conference.statistics.sendMuteEvent(track.isMuted(), type);
  997. });
  998. conference.room.addListener(XMPPEvents.CREATE_OFFER_FAILED, function (e, pc) {
  999. conference.statistics.sendCreateOfferFailed(e, pc);
  1000. });
  1001. conference.room.addListener(XMPPEvents.CREATE_ANSWER_FAILED, function (e, pc) {
  1002. conference.statistics.sendCreateAnswerFailed(e, pc);
  1003. });
  1004. conference.room.addListener(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  1005. function (e, pc) {
  1006. conference.statistics.sendSetLocalDescFailed(e, pc);
  1007. }
  1008. );
  1009. conference.room.addListener(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  1010. function (e, pc) {
  1011. conference.statistics.sendSetRemoteDescFailed(e, pc);
  1012. }
  1013. );
  1014. conference.room.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  1015. function (e, pc) {
  1016. conference.statistics.sendAddIceCandidateFailed(e, pc);
  1017. }
  1018. );
  1019. }
  1020. }
  1021. module.exports = JitsiConference;