您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JitsiConference.js 41KB

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