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

JitsiConference.js 40KB

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