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 35KB

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