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

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