Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

JitsiConference.js 41KB

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