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

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