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

JitsiConference.js 49KB

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