Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JitsiConference.js 49KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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 AuthenticationEvents = require("./service/authentication/AuthenticationEvents");
  7. var RTCEvents = require("./service/RTC/RTCEvents");
  8. var EventEmitter = require("events");
  9. var JitsiConferenceEvents = require("./JitsiConferenceEvents");
  10. var JitsiConferenceErrors = require("./JitsiConferenceErrors");
  11. var JitsiParticipant = require("./JitsiParticipant");
  12. var Statistics = require("./modules/statistics/statistics");
  13. var JitsiDTMFManager = require('./modules/DTMF/JitsiDTMFManager');
  14. var JitsiTrackEvents = require("./JitsiTrackEvents");
  15. var JitsiTrackErrors = require("./JitsiTrackErrors");
  16. var JitsiTrackError = require("./JitsiTrackError");
  17. var Settings = require("./modules/settings/Settings");
  18. var ComponentsVersions = require("./modules/version/ComponentsVersions");
  19. var GlobalOnErrorHandler = require("./modules/util/GlobalOnErrorHandler");
  20. /**
  21. * Creates a JitsiConference object with the given name and properties.
  22. * Note: this constructor is not a part of the public API (objects should be
  23. * created using JitsiConnection.createConference).
  24. * @param options.config properties / settings related to the conference that will be created.
  25. * @param options.name the name of the conference
  26. * @param options.connection the JitsiConnection object for this JitsiConference.
  27. * @constructor
  28. */
  29. function JitsiConference(options) {
  30. if(!options.name || options.name.toLowerCase() !== options.name) {
  31. var errmsg
  32. = "Invalid conference name (no conference name passed or it "
  33. + "contains invalid characters like capital letters)!";
  34. logger.error(errmsg);
  35. throw new Error(errmsg);
  36. }
  37. this.options = options;
  38. this.connection = this.options.connection;
  39. this.xmpp = this.connection.xmpp;
  40. this.eventEmitter = new EventEmitter();
  41. var confID = this.options.name + '@' + this.xmpp.options.hosts.muc;
  42. this.settings = new Settings();
  43. this.room = this.xmpp.createRoom(this.options.name, this.options.config,
  44. this.settings);
  45. this.componentsVersions = new ComponentsVersions(this.room);
  46. this.room.updateDeviceAvailability(RTC.getDeviceAvailability());
  47. this.rtc = new RTC(this.room, options);
  48. this.statistics = new Statistics(this.xmpp, {
  49. callStatsID: this.options.config.callStatsID,
  50. callStatsSecret: this.options.config.callStatsSecret,
  51. disableThirdPartyRequests:
  52. this.options.config.disableThirdPartyRequests,
  53. roomName: this.options.name
  54. });
  55. setupListeners(this);
  56. this.participants = {};
  57. this.lastDominantSpeaker = null;
  58. this.dtmfManager = null;
  59. this.somebodySupportsDTMF = false;
  60. this.authEnabled = false;
  61. this.authIdentity;
  62. this.startAudioMuted = false;
  63. this.startVideoMuted = false;
  64. this.startMutedPolicy = {audio: false, video: false};
  65. this.availableDevices = {
  66. audio: undefined,
  67. video: undefined
  68. };
  69. this.isMutedByFocus = false;
  70. this.reportedAudioSSRCs = {};
  71. }
  72. /**
  73. * Joins the conference.
  74. * @param password {string} the password
  75. */
  76. JitsiConference.prototype.join = function (password) {
  77. if(this.room)
  78. this.room.join(password);
  79. };
  80. /**
  81. * Check if joined to the conference.
  82. */
  83. JitsiConference.prototype.isJoined = function () {
  84. return this.room && this.room.joined;
  85. };
  86. /**
  87. * Leaves the conference and calls onMemberLeft for every participant.
  88. */
  89. JitsiConference.prototype._leaveRoomAndRemoveParticipants = function () {
  90. // leave the conference
  91. if (this.room) {
  92. this.room.leave();
  93. }
  94. this.room = null;
  95. // remove all participants
  96. this.getParticipants().forEach(function (participant) {
  97. this.onMemberLeft(participant.getJid());
  98. }.bind(this));
  99. this.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_LEFT);
  100. }
  101. /**
  102. * Leaves the conference.
  103. * @returns {Promise}
  104. */
  105. JitsiConference.prototype.leave = function () {
  106. var conference = this;
  107. return Promise.all(
  108. conference.getLocalTracks().map(function (track) {
  109. return conference.removeTrack(track);
  110. })
  111. ).then(this._leaveRoomAndRemoveParticipants.bind(this))
  112. .catch(function (error) {
  113. logger.error(error);
  114. GlobalOnErrorHandler.callUnhandledRejectionHandler(
  115. {promise: this, reason: error});
  116. // We are proceeding with leaving the conference because room.leave may
  117. // succeed.
  118. this._leaveRoomAndRemoveParticipants();
  119. return Promise.resolve();
  120. }.bind(this));
  121. };
  122. /**
  123. * Returns name of this conference.
  124. */
  125. JitsiConference.prototype.getName = function () {
  126. return this.options.name;
  127. };
  128. /**
  129. * Check if authentication is enabled for this conference.
  130. */
  131. JitsiConference.prototype.isAuthEnabled = function () {
  132. return this.authEnabled;
  133. };
  134. /**
  135. * Check if user is logged in.
  136. */
  137. JitsiConference.prototype.isLoggedIn = function () {
  138. return !!this.authIdentity;
  139. };
  140. /**
  141. * Get authorized login.
  142. */
  143. JitsiConference.prototype.getAuthLogin = function () {
  144. return this.authIdentity;
  145. };
  146. /**
  147. * Check if external authentication is enabled for this conference.
  148. */
  149. JitsiConference.prototype.isExternalAuthEnabled = function () {
  150. return this.room && this.room.moderator.isExternalAuthEnabled();
  151. };
  152. /**
  153. * Get url for external authentication.
  154. * @param {boolean} [urlForPopup] if true then return url for login popup,
  155. * else url of login page.
  156. * @returns {Promise}
  157. */
  158. JitsiConference.prototype.getExternalAuthUrl = function (urlForPopup) {
  159. return new Promise(function (resolve, reject) {
  160. if (!this.isExternalAuthEnabled()) {
  161. reject();
  162. return;
  163. }
  164. if (urlForPopup) {
  165. this.room.moderator.getPopupLoginUrl(resolve, reject);
  166. } else {
  167. this.room.moderator.getLoginUrl(resolve, reject);
  168. }
  169. }.bind(this));
  170. };
  171. /**
  172. * Returns the local tracks.
  173. */
  174. JitsiConference.prototype.getLocalTracks = function () {
  175. if (this.rtc) {
  176. return this.rtc.localTracks.slice();
  177. } else {
  178. return [];
  179. }
  180. };
  181. /**
  182. * Attaches a handler for events(For example - "participant joined".) in the conference. All possible event are defined
  183. * in JitsiConferenceEvents.
  184. * @param eventId the event ID.
  185. * @param handler handler for the event.
  186. *
  187. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  188. */
  189. JitsiConference.prototype.on = function (eventId, handler) {
  190. if(this.eventEmitter)
  191. this.eventEmitter.on(eventId, handler);
  192. };
  193. /**
  194. * Removes event listener
  195. * @param eventId the event ID.
  196. * @param [handler] optional, the specific handler to unbind
  197. *
  198. * Note: consider adding eventing functionality by extending an EventEmitter impl, instead of rolling ourselves
  199. */
  200. JitsiConference.prototype.off = function (eventId, handler) {
  201. if(this.eventEmitter)
  202. this.eventEmitter.removeListener(eventId, handler);
  203. };
  204. // Common aliases for event emitter
  205. JitsiConference.prototype.addEventListener = JitsiConference.prototype.on;
  206. JitsiConference.prototype.removeEventListener = JitsiConference.prototype.off;
  207. /**
  208. * Receives notifications from other participants about commands / custom events
  209. * (sent by sendCommand or sendCommandOnce methods).
  210. * @param command {String} the name of the command
  211. * @param handler {Function} handler for the command
  212. */
  213. JitsiConference.prototype.addCommandListener = function (command, handler) {
  214. if(this.room)
  215. this.room.addPresenceListener(command, handler);
  216. };
  217. /**
  218. * Removes command listener
  219. * @param command {String} the name of the command
  220. */
  221. JitsiConference.prototype.removeCommandListener = function (command) {
  222. if(this.room)
  223. this.room.removePresenceListener(command);
  224. };
  225. /**
  226. * Sends text message to the other participants in the conference
  227. * @param message the text message.
  228. */
  229. JitsiConference.prototype.sendTextMessage = function (message) {
  230. if(this.room)
  231. this.room.sendMessage(message);
  232. };
  233. /**
  234. * Send presence command.
  235. * @param name {String} the name of the command.
  236. * @param values {Object} with keys and values that will be sent.
  237. **/
  238. JitsiConference.prototype.sendCommand = function (name, values) {
  239. if(this.room) {
  240. this.room.addToPresence(name, values);
  241. this.room.sendPresence();
  242. }
  243. };
  244. /**
  245. * Send presence command one time.
  246. * @param name {String} the name of the command.
  247. * @param values {Object} with keys and values that will be sent.
  248. **/
  249. JitsiConference.prototype.sendCommandOnce = function (name, values) {
  250. this.sendCommand(name, values);
  251. this.removeCommand(name);
  252. };
  253. /**
  254. * Removes presence command.
  255. * @param name {String} the name of the command.
  256. **/
  257. JitsiConference.prototype.removeCommand = function (name) {
  258. if(this.room)
  259. this.room.removeFromPresence(name);
  260. };
  261. /**
  262. * Sets the display name for this conference.
  263. * @param name the display name to set
  264. */
  265. JitsiConference.prototype.setDisplayName = function(name) {
  266. if(this.room){
  267. // remove previously set nickname
  268. this.room.removeFromPresence("nick");
  269. this.room.addToPresence("nick", {attributes: {xmlns: 'http://jabber.org/protocol/nick'}, value: name});
  270. this.room.sendPresence();
  271. }
  272. };
  273. /**
  274. * Set new subject for this conference. (available only for moderator)
  275. * @param {string} subject new subject
  276. */
  277. JitsiConference.prototype.setSubject = function (subject) {
  278. if (this.room && this.isModerator()) {
  279. this.room.setSubject(subject);
  280. }
  281. };
  282. /**
  283. * Adds JitsiLocalTrack object to the conference.
  284. * @param track the JitsiLocalTrack object.
  285. * @returns {Promise<JitsiLocalTrack>}
  286. * @throws {Error} if the specified track is a video track and there is already
  287. * another video track in the conference.
  288. */
  289. JitsiConference.prototype.addTrack = function (track) {
  290. if (track.disposed) {
  291. return Promise.reject(
  292. new JitsiTrackError(JitsiTrackErrors.TRACK_IS_DISPOSED));
  293. }
  294. if (track.isVideoTrack()) {
  295. // Ensure there's exactly 1 local video track in the conference.
  296. var localVideoTrack = this.rtc.getLocalVideoTrack();
  297. if (localVideoTrack) {
  298. // Don't be excessively harsh and severe if the API client happens
  299. // to attempt to add the same local video track twice.
  300. if (track === localVideoTrack) {
  301. return Promise.resolve(track);
  302. } else {
  303. return Promise.reject(new Error(
  304. "cannot add second video track to the conference"));
  305. }
  306. }
  307. }
  308. track.ssrcHandler = function (conference, ssrcMap) {
  309. if(ssrcMap[this.getMSID()]){
  310. this._setSSRC(ssrcMap[this.getMSID()]);
  311. conference.room.removeListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  312. this.ssrcHandler);
  313. }
  314. }.bind(track, this);
  315. this.room.addListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  316. track.ssrcHandler);
  317. if(track.isAudioTrack() || (track.isVideoTrack() &&
  318. track.videoType !== "desktop")) {
  319. // Report active device to statistics
  320. var devices = RTC.getCurrentlyAvailableMediaDevices();
  321. device = devices.find(function (d) {
  322. return d.kind === track.getTrack().kind + 'input'
  323. && d.label === track.getTrack().label;
  324. });
  325. if(device)
  326. Statistics.sendActiveDeviceListEvent(
  327. RTC.getEventDataForActiveDevice(device));
  328. }
  329. return new Promise(function (resolve, reject) {
  330. this.room.addStream(track.getOriginalStream(), function () {
  331. if (track.isVideoTrack()) {
  332. this.removeCommand("videoType");
  333. this.sendCommand("videoType", {
  334. value: track.videoType,
  335. attributes: {
  336. xmlns: 'http://jitsi.org/jitmeet/video'
  337. }
  338. });
  339. }
  340. this.rtc.addLocalTrack(track);
  341. if (track.startMuted) {
  342. track.mute();
  343. }
  344. // ensure that we're sharing proper "is muted" state
  345. if (track.isAudioTrack()) {
  346. this.room.setAudioMute(track.isMuted());
  347. } else {
  348. this.room.setVideoMute(track.isMuted());
  349. }
  350. track.muteHandler = this._fireMuteChangeEvent.bind(this, track);
  351. track.audioLevelHandler = this._fireAudioLevelChangeEvent.bind(this);
  352. track.addEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
  353. track.muteHandler);
  354. track.addEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  355. track.audioLevelHandler);
  356. //FIXME: This dependacy is not necessary. This is quick fix.
  357. track._setConference(this);
  358. // send event for starting screen sharing
  359. // FIXME: we assume we have only one screen sharing track
  360. // if we change this we need to fix this check
  361. if (track.isVideoTrack() && track.videoType === "desktop")
  362. this.statistics.sendScreenSharingEvent(true);
  363. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  364. resolve(track);
  365. }.bind(this), function (error) {
  366. reject(error);
  367. });
  368. }.bind(this));
  369. };
  370. /**
  371. * Fires TRACK_AUDIO_LEVEL_CHANGED change conference event.
  372. * @param audioLevel the audio level
  373. */
  374. JitsiConference.prototype._fireAudioLevelChangeEvent = function (audioLevel) {
  375. this.eventEmitter.emit(
  376. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  377. this.myUserId(), audioLevel);
  378. };
  379. /**
  380. * Fires TRACK_MUTE_CHANGED change conference event.
  381. * @param track the JitsiTrack object related to the event.
  382. */
  383. JitsiConference.prototype._fireMuteChangeEvent = function (track) {
  384. // check if track was muted by focus and now is unmuted by user
  385. if (this.isMutedByFocus && track.isAudioTrack() && !track.isMuted()) {
  386. this.isMutedByFocus = false;
  387. // unmute local user on server
  388. this.room.muteParticipant(this.room.myroomjid, false);
  389. }
  390. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  391. };
  392. /**
  393. * Removes JitsiLocalTrack object to the conference.
  394. * @param track the JitsiLocalTrack object.
  395. * @returns {Promise}
  396. */
  397. JitsiConference.prototype.removeTrack = function (track) {
  398. if (track.disposed) {
  399. return Promise.reject(
  400. new JitsiTrackError(JitsiTrackErrors.TRACK_IS_DISPOSED));
  401. }
  402. if(!this.room){
  403. if(this.rtc) {
  404. this.rtc.removeLocalTrack(track);
  405. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  406. }
  407. return Promise.resolve();
  408. }
  409. return new Promise(function (resolve, reject) {
  410. this.room.removeStream(track.getOriginalStream(), function(){
  411. track._setSSRC(null);
  412. track._setConference(null);
  413. this.rtc.removeLocalTrack(track);
  414. track.removeEventListener(JitsiTrackEvents.TRACK_MUTE_CHANGED,
  415. track.muteHandler);
  416. track.removeEventListener(JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  417. track.audioLevelHandler);
  418. this.room.removeListener(XMPPEvents.SENDRECV_STREAMS_CHANGED,
  419. track.ssrcHandler);
  420. // send event for stopping screen sharing
  421. // FIXME: we assume we have only one screen sharing track
  422. // if we change this we need to fix this check
  423. if (track.isVideoTrack() && track.videoType === "desktop")
  424. this.statistics.sendScreenSharingEvent(false);
  425. this.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  426. resolve();
  427. }.bind(this), function (error) {
  428. reject(error);
  429. }, {
  430. mtype: track.getType(),
  431. type: "remove",
  432. ssrc: track.ssrc});
  433. }.bind(this));
  434. };
  435. /**
  436. * Get role of the local user.
  437. * @returns {string} user role: 'moderator' or 'none'
  438. */
  439. JitsiConference.prototype.getRole = function () {
  440. return this.room.role;
  441. };
  442. /**
  443. * Check if local user is moderator.
  444. * @returns {boolean} true if local user is moderator, false otherwise.
  445. */
  446. JitsiConference.prototype.isModerator = function () {
  447. return this.room.isModerator();
  448. };
  449. /**
  450. * Set password for the room.
  451. * @param {string} password new password for the room.
  452. * @returns {Promise}
  453. */
  454. JitsiConference.prototype.lock = function (password) {
  455. if (!this.isModerator()) {
  456. return Promise.reject();
  457. }
  458. var conference = this;
  459. return new Promise(function (resolve, reject) {
  460. conference.room.lockRoom(password || "", function () {
  461. resolve();
  462. }, function (err) {
  463. reject(err);
  464. }, function () {
  465. reject(JitsiConferenceErrors.PASSWORD_NOT_SUPPORTED);
  466. });
  467. });
  468. };
  469. /**
  470. * Remove password from the room.
  471. * @returns {Promise}
  472. */
  473. JitsiConference.prototype.unlock = function () {
  474. return this.lock();
  475. };
  476. /**
  477. * Elects the participant with the given id to be the selected participant or the speaker.
  478. * @param id the identifier of the participant
  479. */
  480. JitsiConference.prototype.selectParticipant = function(participantId) {
  481. if (this.rtc) {
  482. this.rtc.selectedEndpoint(participantId);
  483. }
  484. };
  485. /**
  486. *
  487. * @param id the identifier of the participant
  488. */
  489. JitsiConference.prototype.pinParticipant = function(participantId) {
  490. if (this.rtc) {
  491. this.rtc.pinEndpoint(participantId);
  492. }
  493. };
  494. /**
  495. * Returns the list of participants for this conference.
  496. * @return Array<JitsiParticipant> a list of participant identifiers containing all conference participants.
  497. */
  498. JitsiConference.prototype.getParticipants = function() {
  499. return Object.keys(this.participants).map(function (key) {
  500. return this.participants[key];
  501. }, this);
  502. };
  503. /**
  504. * @returns {JitsiParticipant} the participant in this conference with the specified id (or
  505. * undefined if there isn't one).
  506. * @param id the id of the participant.
  507. */
  508. JitsiConference.prototype.getParticipantById = function(id) {
  509. return this.participants[id];
  510. };
  511. /**
  512. * Kick participant from this conference.
  513. * @param {string} id id of the participant to kick
  514. */
  515. JitsiConference.prototype.kickParticipant = function (id) {
  516. var participant = this.getParticipantById(id);
  517. if (!participant) {
  518. return;
  519. }
  520. this.room.kick(participant.getJid());
  521. };
  522. /**
  523. * Kick participant from this conference.
  524. * @param {string} id id of the participant to kick
  525. */
  526. JitsiConference.prototype.muteParticipant = function (id) {
  527. var participant = this.getParticipantById(id);
  528. if (!participant) {
  529. return;
  530. }
  531. this.room.muteParticipant(participant.getJid(), true);
  532. };
  533. /**
  534. * Indicates that a participant has joined the conference.
  535. *
  536. * @param jid the jid of the participant in the MUC
  537. * @param nick the display name of the participant
  538. * @param role the role of the participant in the MUC
  539. * @param isHidden indicates if this is a hidden participant (sysem participant,
  540. * for example a recorder).
  541. */
  542. JitsiConference.prototype.onMemberJoined
  543. = function (jid, nick, role, isHidden) {
  544. var id = Strophe.getResourceFromJid(jid);
  545. if (id === 'focus' || this.myUserId() === id) {
  546. return;
  547. }
  548. var participant = new JitsiParticipant(jid, this, nick, isHidden);
  549. participant._role = role;
  550. this.participants[id] = participant;
  551. this.eventEmitter.emit(JitsiConferenceEvents.USER_JOINED, id, participant);
  552. // XXX Since disco is checked in multiple places (e.g.
  553. // modules/xmpp/strophe.jingle.js, modules/xmpp/strophe.rayo.js), check it
  554. // here as well.
  555. var disco = this.xmpp.connection.disco;
  556. if (disco) {
  557. disco.info(
  558. jid, "node", function(iq) {
  559. participant._supportsDTMF = $(iq).find(
  560. '>query>feature[var="urn:xmpp:jingle:dtmf:0"]').length > 0;
  561. this.updateDTMFSupport();
  562. }.bind(this)
  563. );
  564. } else {
  565. // FIXME Should participant._supportsDTMF be assigned false here (and
  566. // this.updateDTMFSupport invoked)?
  567. }
  568. };
  569. JitsiConference.prototype.onMemberLeft = function (jid) {
  570. var id = Strophe.getResourceFromJid(jid);
  571. if (id === 'focus' || this.myUserId() === id) {
  572. return;
  573. }
  574. var participant = this.participants[id];
  575. delete this.participants[id];
  576. this.rtc.removeRemoteTracks(id);
  577. this.eventEmitter.emit(JitsiConferenceEvents.USER_LEFT, id, participant);
  578. };
  579. JitsiConference.prototype.onUserRoleChanged = function (jid, role) {
  580. var id = Strophe.getResourceFromJid(jid);
  581. var participant = this.getParticipantById(id);
  582. if (!participant) {
  583. return;
  584. }
  585. participant._role = role;
  586. this.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, id, role);
  587. };
  588. JitsiConference.prototype.onDisplayNameChanged = function (jid, displayName) {
  589. var id = Strophe.getResourceFromJid(jid);
  590. var participant = this.getParticipantById(id);
  591. if (!participant) {
  592. return;
  593. }
  594. if (participant._displayName === displayName)
  595. return;
  596. participant._displayName = displayName;
  597. this.eventEmitter.emit(JitsiConferenceEvents.DISPLAY_NAME_CHANGED, id, displayName);
  598. };
  599. /**
  600. * Notifies this JitsiConference that a JitsiRemoteTrack was added (into the
  601. * ChatRoom of this JitsiConference).
  602. *
  603. * @param {JitsiRemoteTrack} track the JitsiRemoteTrack which was added to this
  604. * JitsiConference
  605. */
  606. JitsiConference.prototype.onTrackAdded = function (track) {
  607. var id = track.getParticipantId();
  608. var participant = this.getParticipantById(id);
  609. if (!participant) {
  610. return;
  611. }
  612. // Add track to JitsiParticipant.
  613. participant._tracks.push(track);
  614. var emitter = this.eventEmitter;
  615. track.addEventListener(
  616. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  617. function () {
  618. emitter.emit(JitsiConferenceEvents.TRACK_MUTE_CHANGED, track);
  619. }
  620. );
  621. track.addEventListener(
  622. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  623. function (audioLevel) {
  624. emitter.emit(
  625. JitsiConferenceEvents.TRACK_AUDIO_LEVEL_CHANGED,
  626. id,
  627. audioLevel);
  628. }
  629. );
  630. emitter.emit(JitsiConferenceEvents.TRACK_ADDED, track);
  631. };
  632. JitsiConference.prototype.updateDTMFSupport = function () {
  633. var somebodySupportsDTMF = false;
  634. var participants = this.getParticipants();
  635. // check if at least 1 participant supports DTMF
  636. for (var i = 0; i < participants.length; i += 1) {
  637. if (participants[i].supportsDTMF()) {
  638. somebodySupportsDTMF = true;
  639. break;
  640. }
  641. }
  642. if (somebodySupportsDTMF !== this.somebodySupportsDTMF) {
  643. this.somebodySupportsDTMF = somebodySupportsDTMF;
  644. this.eventEmitter.emit(JitsiConferenceEvents.DTMF_SUPPORT_CHANGED, somebodySupportsDTMF);
  645. }
  646. };
  647. /**
  648. * Allows to check if there is at least one user in the conference
  649. * that supports DTMF.
  650. * @returns {boolean} true if somebody supports DTMF, false otherwise
  651. */
  652. JitsiConference.prototype.isDTMFSupported = function () {
  653. return this.somebodySupportsDTMF;
  654. };
  655. /**
  656. * Returns the local user's ID
  657. * @return {string} local user's ID
  658. */
  659. JitsiConference.prototype.myUserId = function () {
  660. return (this.room && this.room.myroomjid)? Strophe.getResourceFromJid(this.room.myroomjid) : null;
  661. };
  662. JitsiConference.prototype.sendTones = function (tones, duration, pause) {
  663. if (!this.dtmfManager) {
  664. var connection = this.xmpp.connection.jingle.activecall.peerconnection;
  665. if (!connection) {
  666. logger.warn("cannot sendTones: no conneciton");
  667. return;
  668. }
  669. var tracks = this.getLocalTracks().filter(function (track) {
  670. return track.isAudioTrack();
  671. });
  672. if (!tracks.length) {
  673. logger.warn("cannot sendTones: no local audio stream");
  674. return;
  675. }
  676. this.dtmfManager = new JitsiDTMFManager(tracks[0], connection);
  677. }
  678. this.dtmfManager.sendTones(tones, duration, pause);
  679. };
  680. /**
  681. * Returns true if the recording is supproted and false if not.
  682. */
  683. JitsiConference.prototype.isRecordingSupported = function () {
  684. if(this.room)
  685. return this.room.isRecordingSupported();
  686. return false;
  687. };
  688. /**
  689. * Returns null if the recording is not supported, "on" if the recording started
  690. * and "off" if the recording is not started.
  691. */
  692. JitsiConference.prototype.getRecordingState = function () {
  693. return (this.room) ? this.room.getRecordingState() : undefined;
  694. }
  695. /**
  696. * Returns the url of the recorded video.
  697. */
  698. JitsiConference.prototype.getRecordingURL = function () {
  699. return (this.room) ? this.room.getRecordingURL() : null;
  700. }
  701. /**
  702. * Starts/stops the recording
  703. */
  704. JitsiConference.prototype.toggleRecording = function (options) {
  705. if(this.room)
  706. return this.room.toggleRecording(options, function (status, error) {
  707. this.eventEmitter.emit(
  708. JitsiConferenceEvents.RECORDER_STATE_CHANGED, status, error);
  709. }.bind(this));
  710. this.eventEmitter.emit(
  711. JitsiConferenceEvents.RECORDER_STATE_CHANGED, "error",
  712. new Error("The conference is not created yet!"));
  713. }
  714. /**
  715. * Returns true if the SIP calls are supported and false otherwise
  716. */
  717. JitsiConference.prototype.isSIPCallingSupported = function () {
  718. if(this.room)
  719. return this.room.isSIPCallingSupported();
  720. return false;
  721. }
  722. /**
  723. * Dials a number.
  724. * @param number the number
  725. */
  726. JitsiConference.prototype.dial = function (number) {
  727. if(this.room)
  728. return this.room.dial(number);
  729. return new Promise(function(resolve, reject){
  730. reject(new Error("The conference is not created yet!"))});
  731. }
  732. /**
  733. * Hangup an existing call
  734. */
  735. JitsiConference.prototype.hangup = function () {
  736. if(this.room)
  737. return this.room.hangup();
  738. return new Promise(function(resolve, reject){
  739. reject(new Error("The conference is not created yet!"))});
  740. }
  741. /**
  742. * Returns the phone number for joining the conference.
  743. */
  744. JitsiConference.prototype.getPhoneNumber = function () {
  745. if(this.room)
  746. return this.room.getPhoneNumber();
  747. return null;
  748. }
  749. /**
  750. * Returns the pin for joining the conference with phone.
  751. */
  752. JitsiConference.prototype.getPhonePin = function () {
  753. if(this.room)
  754. return this.room.getPhonePin();
  755. return null;
  756. }
  757. /**
  758. * Returns the connection state for the current room. Its ice connection state
  759. * for its session.
  760. */
  761. JitsiConference.prototype.getConnectionState = function () {
  762. if(this.room)
  763. return this.room.getConnectionState();
  764. return null;
  765. }
  766. /**
  767. * Make all new participants mute their audio/video on join.
  768. * @param policy {Object} object with 2 boolean properties for video and audio:
  769. * @param {boolean} audio if audio should be muted.
  770. * @param {boolean} video if video should be muted.
  771. */
  772. JitsiConference.prototype.setStartMutedPolicy = function (policy) {
  773. if (!this.isModerator()) {
  774. return;
  775. }
  776. this.startMutedPolicy = policy;
  777. this.room.removeFromPresence("startmuted");
  778. this.room.addToPresence("startmuted", {
  779. attributes: {
  780. audio: policy.audio,
  781. video: policy.video,
  782. xmlns: 'http://jitsi.org/jitmeet/start-muted'
  783. }
  784. });
  785. this.room.sendPresence();
  786. };
  787. /**
  788. * Returns current start muted policy
  789. * @returns {Object} with 2 proprties - audio and video.
  790. */
  791. JitsiConference.prototype.getStartMutedPolicy = function () {
  792. return this.startMutedPolicy;
  793. };
  794. /**
  795. * Check if audio is muted on join.
  796. */
  797. JitsiConference.prototype.isStartAudioMuted = function () {
  798. return this.startAudioMuted;
  799. };
  800. /**
  801. * Check if video is muted on join.
  802. */
  803. JitsiConference.prototype.isStartVideoMuted = function () {
  804. return this.startVideoMuted;
  805. };
  806. /**
  807. * Get object with internal logs.
  808. */
  809. JitsiConference.prototype.getLogs = function () {
  810. var data = this.xmpp.getJingleLog();
  811. var metadata = {};
  812. metadata.time = new Date();
  813. metadata.url = window.location.href;
  814. metadata.ua = navigator.userAgent;
  815. var log = this.xmpp.getXmppLog();
  816. if (log) {
  817. metadata.xmpp = log;
  818. }
  819. data.metadata = metadata;
  820. return data;
  821. };
  822. /**
  823. * Returns measured connectionTimes.
  824. */
  825. JitsiConference.prototype.getConnectionTimes = function () {
  826. return this.room.connectionTimes;
  827. };
  828. /**
  829. * Sets a property for the local participant.
  830. */
  831. JitsiConference.prototype.setLocalParticipantProperty = function(name, value) {
  832. this.sendCommand("jitsi_participant_" + name, {value: value});
  833. };
  834. /**
  835. * Sends the given feedback through CallStats if enabled.
  836. *
  837. * @param overallFeedback an integer between 1 and 5 indicating the
  838. * user feedback
  839. * @param detailedFeedback detailed feedback from the user. Not yet used
  840. */
  841. JitsiConference.prototype.sendFeedback =
  842. function(overallFeedback, detailedFeedback){
  843. this.statistics.sendFeedback(overallFeedback, detailedFeedback);
  844. }
  845. /**
  846. * Returns true if the callstats integration is enabled, otherwise returns
  847. * false.
  848. *
  849. * @returns true if the callstats integration is enabled, otherwise returns
  850. * false.
  851. */
  852. JitsiConference.prototype.isCallstatsEnabled = function () {
  853. return this.statistics.isCallstatsEnabled();
  854. }
  855. /**
  856. * Reports detected audio problem with the media stream related to the passed
  857. * ssrc.
  858. * @param ssrc {string} the ssrc
  859. */
  860. JitsiConference.prototype._reportAudioProblem = function (ssrc) {
  861. if(this.reportedAudioSSRCs[ssrc])
  862. return;
  863. var track = this.rtc.getRemoteTrackBySSRC(ssrc);
  864. if(!track || !track.isAudioTrack())
  865. return;
  866. this.reportedAudioSSRCs[ssrc] = true;
  867. var errorContent = {
  868. errMsg: "The audio is received but not played",
  869. ssrc: ssrc
  870. };
  871. var mstream = track.stream, mtrack = track.track;
  872. if(mstream) {
  873. errorContent.MediaStream = {
  874. active: mstream.active,
  875. id: mstream.id
  876. }
  877. }
  878. if(mtrack) {
  879. errorContent.MediaStreamTrack = {
  880. enabled: mtrack.enabled,
  881. id: mtrack.id,
  882. label: mtrack.label,
  883. muted: mtrack.muted
  884. }
  885. }
  886. if(track.containers) {
  887. errorContent.containers = [];
  888. track.containers.forEach(function (container) {
  889. errorContent.containers.push({
  890. autoplay: container.autoplay,
  891. muted: container.muted,
  892. src: container.src,
  893. volume: container.volume,
  894. id: container.id,
  895. ended: container.ended,
  896. paused: container.paused,
  897. readyState: container.readyState
  898. });
  899. });
  900. }
  901. this.statistics.sendDetectedAudioProblem(
  902. new Error(JSON.stringify(errorContent)));
  903. logger.error("Audio problem detected. The audio is received but not played",
  904. errorContent);
  905. }
  906. /**
  907. * Setups the listeners needed for the conference.
  908. * @param conference the conference
  909. */
  910. function setupListeners(conference) {
  911. conference.xmpp.addListener(
  912. XMPPEvents.CALL_INCOMING, function (jingleSession, jingleOffer, now) {
  913. if (conference.room.isFocus(jingleSession.peerjid)) {
  914. // Accept incoming call
  915. conference.room.setJingleSession(jingleSession);
  916. conference.room.connectionTimes["session.initiate"] = now;
  917. try{
  918. jingleSession.initialize(false /* initiator */,
  919. conference.room);
  920. } catch (error) {
  921. GlobalOnErrorHandler.callErrorHandler(error);
  922. };
  923. conference.rtc.onIncommingCall(jingleSession);
  924. jingleSession.acceptOffer(jingleOffer, null,
  925. function (error) {
  926. GlobalOnErrorHandler.callErrorHandler(error);
  927. logger.error(
  928. "Failed to accept incoming Jingle session", error);
  929. }
  930. );
  931. // Start callstats as soon as peerconnection is initialized,
  932. // do not wait for XMPPEvents.PEERCONNECTION_READY, as it may never
  933. // happen in case if user doesn't have or denied permission to
  934. // both camera and microphone.
  935. conference.statistics.startCallStats(jingleSession, conference.settings);
  936. conference.statistics.startRemoteStats(jingleSession.peerconnection);
  937. } else {
  938. // Error cause this should never happen unless something is wrong!
  939. var errmsg
  940. = "Rejecting session-initiate from non-focus user: "
  941. + jingleSession.peerjid;
  942. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  943. logger.error(errmsg);
  944. }
  945. });
  946. conference.room.addListener(XMPPEvents.ICE_RESTARTING, function () {
  947. // All data channels have to be closed, before ICE restart
  948. // otherwise Chrome will not trigger "opened" event for the channel
  949. // established with the new bridge
  950. conference.rtc.closeAllDataChannels();
  951. });
  952. conference.room.addListener(XMPPEvents.LOCAL_UFRAG_CHANGED, function (ufrag) {
  953. Statistics.sendLog("Local ufrag: " + ufrag);
  954. });
  955. conference.room.addListener(XMPPEvents.REMOTE_UFRAG_CHANGED, function (ufrag) {
  956. Statistics.sendLog("Remote ufrag: " + ufrag);
  957. });
  958. conference.room.addListener(XMPPEvents.REMOTE_TRACK_ADDED,
  959. function (data) {
  960. var track = conference.rtc.createRemoteTrack(data);
  961. if (track) {
  962. conference.onTrackAdded(track);
  963. }
  964. }
  965. );
  966. conference.room.addListener(XMPPEvents.REMOTE_TRACK_REMOVED,
  967. function (streamId, trackId) {
  968. conference.getParticipants().forEach(function(participant) {
  969. var tracks = participant.getTracks();
  970. for(var i = 0; i < tracks.length; i++) {
  971. if(tracks[i]
  972. && tracks[i].getStreamId() == streamId
  973. && tracks[i].getTrackId() == trackId) {
  974. var track = participant._tracks.splice(i, 1)[0];
  975. conference.eventEmitter.emit(
  976. JitsiConferenceEvents.TRACK_REMOVED, track);
  977. return;
  978. }
  979. }
  980. });
  981. }
  982. );
  983. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  984. function (value) {
  985. // set isMutedByFocus when setAudioMute Promise ends
  986. conference.rtc.setAudioMute(value).then(
  987. function() {
  988. conference.isMutedByFocus = true;
  989. },
  990. function() {
  991. logger.warn(
  992. "Error while audio muting due to focus request");
  993. });
  994. }
  995. );
  996. conference.room.addListener(XMPPEvents.SUBJECT_CHANGED, function (subject) {
  997. conference.eventEmitter.emit(JitsiConferenceEvents.SUBJECT_CHANGED,
  998. subject);
  999. });
  1000. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  1001. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  1002. });
  1003. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  1004. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1005. JitsiConferenceErrors.CONNECTION_ERROR, pres);
  1006. });
  1007. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  1008. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1009. JitsiConferenceErrors.CONNECTION_ERROR, pres);
  1010. });
  1011. conference.room.addListener(XMPPEvents.ROOM_MAX_USERS_ERROR,
  1012. function (pres) {
  1013. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1014. JitsiConferenceErrors.CONFERENCE_MAX_USERS, pres);
  1015. });
  1016. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  1017. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  1018. });
  1019. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  1020. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  1021. });
  1022. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  1023. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  1024. });
  1025. conference.room.addListener(XMPPEvents.RESERVATION_ERROR, function (code, msg) {
  1026. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.RESERVATION_ERROR, code, msg);
  1027. });
  1028. conference.room.addListener(XMPPEvents.GRACEFUL_SHUTDOWN, function () {
  1029. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  1030. });
  1031. conference.room.addListener(XMPPEvents.JINGLE_FATAL_ERROR, function (session, error) {
  1032. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.JINGLE_FATAL_ERROR, error);
  1033. });
  1034. conference.room.addListener(XMPPEvents.MUC_DESTROYED, function (reason) {
  1035. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONFERENCE_DESTROYED, reason);
  1036. });
  1037. conference.room.addListener(XMPPEvents.CHAT_ERROR_RECEIVED, function (err, msg) {
  1038. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_ERROR, JitsiConferenceErrors.CHAT_ERROR, err, msg);
  1039. });
  1040. conference.room.addListener(XMPPEvents.FOCUS_DISCONNECTED, function (focus, retrySec) {
  1041. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_DISCONNECTED, focus, retrySec);
  1042. });
  1043. conference.room.addListener(XMPPEvents.FOCUS_LEFT, function () {
  1044. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_LEFT);
  1045. });
  1046. conference.room.setParticipantPropertyListener(function (node, from) {
  1047. var participant = conference.getParticipantById(from);
  1048. if (!participant) {
  1049. return;
  1050. }
  1051. participant.setProperty(
  1052. node.tagName.substring("jitsi_participant_".length),
  1053. node.value);
  1054. });
  1055. conference.room.addListener(XMPPEvents.KICKED, function () {
  1056. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  1057. });
  1058. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  1059. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  1060. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  1061. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  1062. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  1063. // log all events for the recorder operated by the moderator
  1064. if (conference.statistics && conference.isModerator()) {
  1065. conference.on(JitsiConferenceEvents.RECORDER_STATE_CHANGED,
  1066. function (status, error) {
  1067. Statistics.sendLog(
  1068. "[Recorder] status: " + status
  1069. + (error? " error: " + error : ""));
  1070. });
  1071. }
  1072. });
  1073. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  1074. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  1075. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  1076. });
  1077. conference.room.addListener(XMPPEvents.RECORDER_STATE_CHANGED,
  1078. function (state) {
  1079. conference.eventEmitter.emit(
  1080. JitsiConferenceEvents.RECORDER_STATE_CHANGED, state);
  1081. });
  1082. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  1083. conference.eventEmitter.emit(
  1084. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  1085. });
  1086. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  1087. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  1088. });
  1089. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED,
  1090. function (error) {
  1091. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1092. JitsiConferenceErrors.SETUP_FAILED, error);
  1093. });
  1094. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  1095. conference.authEnabled = authEnabled;
  1096. conference.authIdentity = authIdentity;
  1097. conference.eventEmitter.emit(JitsiConferenceEvents.AUTH_STATUS_CHANGED, authEnabled, authIdentity);
  1098. });
  1099. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  1100. var id = Strophe.getResourceFromJid(jid);
  1101. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  1102. });
  1103. conference.room.addListener(XMPPEvents.PRESENCE_STATUS, function (jid, status) {
  1104. var id = Strophe.getResourceFromJid(jid);
  1105. var participant = conference.getParticipantById(id);
  1106. if (!participant || participant._status === status) {
  1107. return;
  1108. }
  1109. participant._status = status;
  1110. conference.eventEmitter.emit(JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  1111. });
  1112. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  1113. if(conference.lastDominantSpeaker !== id && conference.room) {
  1114. conference.lastDominantSpeaker = id;
  1115. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  1116. }
  1117. if (conference.statistics && conference.myUserId() === id) {
  1118. // We are the new dominant speaker.
  1119. conference.statistics.sendDominantSpeakerEvent();
  1120. }
  1121. });
  1122. conference.rtc.addListener(RTCEvents.DATA_CHANNEL_OPEN, function () {
  1123. var now = window.performance.now();
  1124. logger.log("(TIME) data channel opened ", now);
  1125. conference.room.connectionTimes["data.channel.opened"] = now;
  1126. });
  1127. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  1128. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  1129. });
  1130. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  1131. function (lastNEndpoints, endpointsEnteringLastN) {
  1132. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  1133. lastNEndpoints, endpointsEnteringLastN);
  1134. });
  1135. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  1136. function (audioMuted, videoMuted) {
  1137. conference.startAudioMuted = audioMuted;
  1138. conference.startVideoMuted = videoMuted;
  1139. // mute existing local tracks because this is initial mute from
  1140. // Jicofo
  1141. conference.getLocalTracks().forEach(function (track) {
  1142. if (conference.startAudioMuted && track.isAudioTrack()) {
  1143. track.mute();
  1144. }
  1145. if (conference.startVideoMuted && track.isVideoTrack()) {
  1146. track.mute();
  1147. }
  1148. });
  1149. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  1150. });
  1151. conference.room.addPresenceListener("startmuted", function (data, from) {
  1152. var isModerator = false;
  1153. if (conference.myUserId() === from && conference.isModerator()) {
  1154. isModerator = true;
  1155. } else {
  1156. var participant = conference.getParticipantById(from);
  1157. if (participant && participant.isModerator()) {
  1158. isModerator = true;
  1159. }
  1160. }
  1161. if (!isModerator) {
  1162. return;
  1163. }
  1164. var startAudioMuted = data.attributes.audio === 'true';
  1165. var startVideoMuted = data.attributes.video === 'true';
  1166. var updated = false;
  1167. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  1168. conference.startMutedPolicy.audio = startAudioMuted;
  1169. updated = true;
  1170. }
  1171. if (startVideoMuted !== conference.startMutedPolicy.video) {
  1172. conference.startMutedPolicy.video = startVideoMuted;
  1173. updated = true;
  1174. }
  1175. if (updated) {
  1176. conference.eventEmitter.emit(
  1177. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  1178. conference.startMutedPolicy
  1179. );
  1180. }
  1181. });
  1182. conference.rtc.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  1183. conference.room.updateDeviceAvailability(devices);
  1184. });
  1185. conference.room.addPresenceListener("devices", function (data, from) {
  1186. var isAudioAvailable = false;
  1187. var isVideoAvailable = false;
  1188. data.children.forEach(function (config) {
  1189. if (config.tagName === 'audio') {
  1190. isAudioAvailable = config.value === 'true';
  1191. }
  1192. if (config.tagName === 'video') {
  1193. isVideoAvailable = config.value === 'true';
  1194. }
  1195. });
  1196. var availableDevices;
  1197. if (conference.myUserId() === from) {
  1198. availableDevices = conference.availableDevices;
  1199. } else {
  1200. var participant = conference.getParticipantById(from);
  1201. if (!participant) {
  1202. return;
  1203. }
  1204. availableDevices = participant._availableDevices;
  1205. }
  1206. var updated = false;
  1207. if (availableDevices.audio !== isAudioAvailable) {
  1208. updated = true;
  1209. availableDevices.audio = isAudioAvailable;
  1210. }
  1211. if (availableDevices.video !== isVideoAvailable) {
  1212. updated = true;
  1213. availableDevices.video = isVideoAvailable;
  1214. }
  1215. if (updated) {
  1216. conference.eventEmitter.emit(
  1217. JitsiConferenceEvents.AVAILABLE_DEVICES_CHANGED,
  1218. from, availableDevices);
  1219. }
  1220. });
  1221. if(conference.statistics) {
  1222. //FIXME: Maybe remove event should not be associated with the conference.
  1223. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  1224. var userId = null;
  1225. var resource = conference.rtc.getResourceBySSRC(ssrc);
  1226. if (!resource)
  1227. return;
  1228. conference.rtc.setAudioLevel(resource, level);
  1229. });
  1230. conference.statistics.addAudioProblemListener(function (ssrc) {
  1231. conference._reportAudioProblem(ssrc)
  1232. });
  1233. conference.statistics.addConnectionStatsListener(function (stats) {
  1234. var ssrc2resolution = stats.resolution;
  1235. var id2resolution = {};
  1236. // preprocess resolutions: group by user id, skip incorrect
  1237. // resolutions etc.
  1238. Object.keys(ssrc2resolution).forEach(function (ssrc) {
  1239. var resolution = ssrc2resolution[ssrc];
  1240. if (!resolution.width || !resolution.height ||
  1241. resolution.width == -1 || resolution.height == -1) {
  1242. return;
  1243. }
  1244. var id = conference.rtc.getResourceBySSRC(ssrc);
  1245. if (!id) {
  1246. return;
  1247. }
  1248. // ssrc to resolution map for user id
  1249. var idResolutions = id2resolution[id] || {};
  1250. idResolutions[ssrc] = resolution;
  1251. id2resolution[id] = idResolutions;
  1252. });
  1253. stats.resolution = id2resolution;
  1254. conference.eventEmitter.emit(
  1255. JitsiConferenceEvents.CONNECTION_STATS, stats);
  1256. });
  1257. conference.room.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  1258. function () {
  1259. conference.statistics.dispose();
  1260. });
  1261. conference.room.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
  1262. function (pc) {
  1263. conference.statistics.sendIceConnectionFailedEvent(pc);
  1264. conference.room.eventEmitter.emit(
  1265. XMPPEvents.CONFERENCE_SETUP_FAILED,
  1266. new Error("ICE fail"));
  1267. });
  1268. conference.rtc.addListener(RTCEvents.TRACK_ATTACHED,
  1269. function(track, container) {
  1270. var ssrc = track.getSSRC();
  1271. if (!container.id || !ssrc) {
  1272. return;
  1273. }
  1274. conference.statistics.associateStreamWithVideoTag(
  1275. ssrc, track.isLocal(), track.getUsageLabel(), container.id);
  1276. });
  1277. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  1278. function (track) {
  1279. if(!track.isLocal())
  1280. return;
  1281. var type = (track.getType() === "audio")? "audio" : "video";
  1282. conference.statistics.sendMuteEvent(track.isMuted(), type);
  1283. });
  1284. conference.room.addListener(XMPPEvents.CREATE_OFFER_FAILED, function (e, pc) {
  1285. conference.statistics.sendCreateOfferFailed(e, pc);
  1286. });
  1287. conference.room.addListener(XMPPEvents.CREATE_ANSWER_FAILED, function (e, pc) {
  1288. conference.statistics.sendCreateAnswerFailed(e, pc);
  1289. });
  1290. conference.room.addListener(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  1291. function (e, pc) {
  1292. conference.statistics.sendSetLocalDescFailed(e, pc);
  1293. }
  1294. );
  1295. conference.room.addListener(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  1296. function (e, pc) {
  1297. conference.statistics.sendSetRemoteDescFailed(e, pc);
  1298. }
  1299. );
  1300. conference.room.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  1301. function (e, pc) {
  1302. conference.statistics.sendAddIceCandidateFailed(e, pc);
  1303. }
  1304. );
  1305. }
  1306. }
  1307. module.exports = JitsiConference;