You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JitsiConference.js 51KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  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.selectEndpoint(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. * NOTE: all logger.log calls are there only to be able to see the info in
  860. * torture
  861. */
  862. JitsiConference.prototype._reportAudioProblem = function (ssrc) {
  863. if(this.reportedAudioSSRCs[ssrc])
  864. return;
  865. var track = this.rtc.getRemoteTrackBySSRC(ssrc);
  866. if(!track || !track.isAudioTrack())
  867. return;
  868. var id = track.getParticipantId();
  869. var displayName = null;
  870. if(id) {
  871. var participant = this.getParticipantById(id);
  872. if(participant) {
  873. displayName = participant.getDisplayName();
  874. }
  875. }
  876. this.reportedAudioSSRCs[ssrc] = true;
  877. var errorContent = {
  878. errMsg: "The audio is received but not played",
  879. ssrc: ssrc,
  880. jid: id,
  881. displayName: displayName
  882. };
  883. logger.log("=================The audio is received but not played" +
  884. "======================");
  885. logger.log("ssrc: ", ssrc);
  886. logger.log("jid: ", id);
  887. logger.log("displayName: ", displayName);
  888. var mstream = track.stream, mtrack = track.track;
  889. if(mstream) {
  890. logger.log("MediaStream:");
  891. errorContent.MediaStream = {
  892. active: mstream.active,
  893. id: mstream.id
  894. };
  895. logger.log("active: ", mstream.active);
  896. logger.log("id: ", mstream.id);
  897. }
  898. if(mtrack) {
  899. logger.log("MediaStreamTrack:");
  900. errorContent.MediaStreamTrack = {
  901. enabled: mtrack.enabled,
  902. id: mtrack.id,
  903. label: mtrack.label,
  904. muted: mtrack.muted
  905. }
  906. logger.log("enabled: ", mtrack.enabled);
  907. logger.log("id: ", mtrack.id);
  908. logger.log("label: ", mtrack.label);
  909. logger.log("muted: ", mtrack.muted);
  910. }
  911. if(track.containers) {
  912. errorContent.containers = [];
  913. logger.log("Containers:");
  914. track.containers.forEach(function (container) {
  915. logger.log("Container:");
  916. errorContent.containers.push({
  917. autoplay: container.autoplay,
  918. muted: container.muted,
  919. src: container.src,
  920. volume: container.volume,
  921. id: container.id,
  922. ended: container.ended,
  923. paused: container.paused,
  924. readyState: container.readyState
  925. });
  926. logger.log("autoplay: ", container.autoplay);
  927. logger.log("muted: ", container.muted);
  928. logger.log("src: ", container.src);
  929. logger.log("volume: ", container.volume);
  930. logger.log("id: ", container.id);
  931. logger.log("ended: ", container.ended);
  932. logger.log("paused: ", container.paused);
  933. logger.log("readyState: ", container.readyState);
  934. });
  935. }
  936. this.statistics.sendDetectedAudioProblem(
  937. new Error(JSON.stringify(errorContent)));
  938. // Prints JSON.stringify(errorContent) to be able to see all properties of
  939. // errorContent from torture
  940. logger.error("Audio problem detected. The audio is received but not played",
  941. errorContent);
  942. };
  943. /**
  944. * Logs an "application log" message
  945. */
  946. JitsiConference.prototype.sendApplicationLog = function(message) {
  947. Statistics.sendLog(message);
  948. };
  949. /**
  950. * Setups the listeners needed for the conference.
  951. * @param conference the conference
  952. */
  953. function setupListeners(conference) {
  954. conference.xmpp.addListener(
  955. XMPPEvents.CALL_INCOMING, function (jingleSession, jingleOffer, now) {
  956. if (conference.room.isFocus(jingleSession.peerjid)) {
  957. // Accept incoming call
  958. conference.room.setJingleSession(jingleSession);
  959. conference.room.connectionTimes["session.initiate"] = now;
  960. try{
  961. jingleSession.initialize(false /* initiator */,
  962. conference.room);
  963. } catch (error) {
  964. GlobalOnErrorHandler.callErrorHandler(error);
  965. };
  966. conference.rtc.onIncommingCall(jingleSession);
  967. jingleSession.acceptOffer(jingleOffer, null,
  968. function (error) {
  969. GlobalOnErrorHandler.callErrorHandler(error);
  970. logger.error(
  971. "Failed to accept incoming Jingle session", error);
  972. }
  973. );
  974. // Start callstats as soon as peerconnection is initialized,
  975. // do not wait for XMPPEvents.PEERCONNECTION_READY, as it may never
  976. // happen in case if user doesn't have or denied permission to
  977. // both camera and microphone.
  978. conference.statistics.startCallStats(jingleSession, conference.settings);
  979. conference.statistics.startRemoteStats(jingleSession.peerconnection);
  980. } else {
  981. // Error cause this should never happen unless something is wrong!
  982. var errmsg
  983. = "Rejecting session-initiate from non-focus user: "
  984. + jingleSession.peerjid;
  985. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  986. logger.error(errmsg);
  987. }
  988. });
  989. conference.room.addListener(XMPPEvents.ICE_RESTARTING, function () {
  990. // All data channels have to be closed, before ICE restart
  991. // otherwise Chrome will not trigger "opened" event for the channel
  992. // established with the new bridge
  993. conference.rtc.closeAllDataChannels();
  994. });
  995. conference.room.addListener(XMPPEvents.LOCAL_UFRAG_CHANGED, function (ufrag) {
  996. Statistics.sendLog("Local ufrag: " + ufrag);
  997. });
  998. conference.room.addListener(XMPPEvents.REMOTE_UFRAG_CHANGED, function (ufrag) {
  999. Statistics.sendLog("Remote ufrag: " + ufrag);
  1000. });
  1001. conference.room.addListener(XMPPEvents.REMOTE_TRACK_ADDED,
  1002. function (data) {
  1003. var track = conference.rtc.createRemoteTrack(data);
  1004. if (track) {
  1005. conference.onTrackAdded(track);
  1006. }
  1007. }
  1008. );
  1009. conference.room.addListener(XMPPEvents.REMOTE_TRACK_REMOVED,
  1010. function (streamId, trackId) {
  1011. conference.getParticipants().forEach(function(participant) {
  1012. var tracks = participant.getTracks();
  1013. for(var i = 0; i < tracks.length; i++) {
  1014. if(tracks[i]
  1015. && tracks[i].getStreamId() == streamId
  1016. && tracks[i].getTrackId() == trackId) {
  1017. var track = participant._tracks.splice(i, 1)[0];
  1018. conference.eventEmitter.emit(
  1019. JitsiConferenceEvents.TRACK_REMOVED, track);
  1020. return;
  1021. }
  1022. }
  1023. });
  1024. }
  1025. );
  1026. conference.room.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  1027. function (value) {
  1028. // set isMutedByFocus when setAudioMute Promise ends
  1029. conference.rtc.setAudioMute(value).then(
  1030. function() {
  1031. conference.isMutedByFocus = true;
  1032. },
  1033. function() {
  1034. logger.warn(
  1035. "Error while audio muting due to focus request");
  1036. });
  1037. }
  1038. );
  1039. conference.room.addListener(XMPPEvents.SUBJECT_CHANGED, function (subject) {
  1040. conference.eventEmitter.emit(JitsiConferenceEvents.SUBJECT_CHANGED,
  1041. subject);
  1042. });
  1043. conference.room.addListener(XMPPEvents.MUC_JOINED, function () {
  1044. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_JOINED);
  1045. });
  1046. conference.room.addListener(XMPPEvents.ROOM_JOIN_ERROR, function (pres) {
  1047. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1048. JitsiConferenceErrors.CONNECTION_ERROR, pres);
  1049. });
  1050. conference.room.addListener(XMPPEvents.ROOM_CONNECT_ERROR, function (pres) {
  1051. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1052. JitsiConferenceErrors.CONNECTION_ERROR, pres);
  1053. });
  1054. conference.room.addListener(XMPPEvents.ROOM_MAX_USERS_ERROR,
  1055. function (pres) {
  1056. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1057. JitsiConferenceErrors.CONFERENCE_MAX_USERS, pres);
  1058. });
  1059. conference.room.addListener(XMPPEvents.PASSWORD_REQUIRED, function (pres) {
  1060. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.PASSWORD_REQUIRED, pres);
  1061. });
  1062. conference.room.addListener(XMPPEvents.AUTHENTICATION_REQUIRED, function () {
  1063. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  1064. });
  1065. conference.room.addListener(XMPPEvents.BRIDGE_DOWN, function () {
  1066. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  1067. });
  1068. conference.room.addListener(XMPPEvents.RESERVATION_ERROR, function (code, msg) {
  1069. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.RESERVATION_ERROR, code, msg);
  1070. });
  1071. conference.room.addListener(XMPPEvents.GRACEFUL_SHUTDOWN, function () {
  1072. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  1073. });
  1074. conference.room.addListener(XMPPEvents.JINGLE_FATAL_ERROR, function (session, error) {
  1075. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.JINGLE_FATAL_ERROR, error);
  1076. });
  1077. conference.room.addListener(XMPPEvents.MUC_DESTROYED, function (reason) {
  1078. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.CONFERENCE_DESTROYED, reason);
  1079. });
  1080. conference.room.addListener(XMPPEvents.CHAT_ERROR_RECEIVED, function (err, msg) {
  1081. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_ERROR, JitsiConferenceErrors.CHAT_ERROR, err, msg);
  1082. });
  1083. conference.room.addListener(XMPPEvents.FOCUS_DISCONNECTED, function (focus, retrySec) {
  1084. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_DISCONNECTED, focus, retrySec);
  1085. });
  1086. conference.room.addListener(XMPPEvents.FOCUS_LEFT, function () {
  1087. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED, JitsiConferenceErrors.FOCUS_LEFT);
  1088. });
  1089. conference.room.setParticipantPropertyListener(function (node, from) {
  1090. var participant = conference.getParticipantById(from);
  1091. if (!participant) {
  1092. return;
  1093. }
  1094. participant.setProperty(
  1095. node.tagName.substring("jitsi_participant_".length),
  1096. node.value);
  1097. });
  1098. conference.room.addListener(XMPPEvents.KICKED, function () {
  1099. conference.eventEmitter.emit(JitsiConferenceEvents.KICKED);
  1100. });
  1101. conference.room.addListener(XMPPEvents.MUC_MEMBER_JOINED, conference.onMemberJoined.bind(conference));
  1102. conference.room.addListener(XMPPEvents.MUC_MEMBER_LEFT, conference.onMemberLeft.bind(conference));
  1103. conference.room.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, conference.onDisplayNameChanged.bind(conference));
  1104. conference.room.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, function (role) {
  1105. conference.eventEmitter.emit(JitsiConferenceEvents.USER_ROLE_CHANGED, conference.myUserId(), role);
  1106. // log all events for the recorder operated by the moderator
  1107. if (conference.statistics && conference.isModerator()) {
  1108. conference.on(JitsiConferenceEvents.RECORDER_STATE_CHANGED,
  1109. function (status, error) {
  1110. Statistics.sendLog(
  1111. "[Recorder] status: " + status
  1112. + (error? " error: " + error : ""));
  1113. });
  1114. }
  1115. });
  1116. conference.room.addListener(XMPPEvents.MUC_ROLE_CHANGED, conference.onUserRoleChanged.bind(conference));
  1117. conference.room.addListener(XMPPEvents.CONNECTION_INTERRUPTED, function () {
  1118. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_INTERRUPTED);
  1119. });
  1120. conference.room.addListener(XMPPEvents.RECORDER_STATE_CHANGED,
  1121. function (state) {
  1122. conference.eventEmitter.emit(
  1123. JitsiConferenceEvents.RECORDER_STATE_CHANGED, state);
  1124. });
  1125. conference.room.addListener(XMPPEvents.PHONE_NUMBER_CHANGED, function () {
  1126. conference.eventEmitter.emit(
  1127. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  1128. });
  1129. conference.room.addListener(XMPPEvents.CONNECTION_RESTORED, function () {
  1130. conference.eventEmitter.emit(JitsiConferenceEvents.CONNECTION_RESTORED);
  1131. });
  1132. conference.room.addListener(XMPPEvents.CONFERENCE_SETUP_FAILED,
  1133. function (error) {
  1134. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  1135. JitsiConferenceErrors.SETUP_FAILED, error);
  1136. });
  1137. conference.room.addListener(AuthenticationEvents.IDENTITY_UPDATED, function (authEnabled, authIdentity) {
  1138. conference.authEnabled = authEnabled;
  1139. conference.authIdentity = authIdentity;
  1140. conference.eventEmitter.emit(JitsiConferenceEvents.AUTH_STATUS_CHANGED, authEnabled, authIdentity);
  1141. });
  1142. conference.room.addListener(XMPPEvents.MESSAGE_RECEIVED, function (jid, displayName, txt, myJid, ts) {
  1143. var id = Strophe.getResourceFromJid(jid);
  1144. conference.eventEmitter.emit(JitsiConferenceEvents.MESSAGE_RECEIVED, id, txt, ts);
  1145. });
  1146. conference.room.addListener(XMPPEvents.PRESENCE_STATUS, function (jid, status) {
  1147. var id = Strophe.getResourceFromJid(jid);
  1148. var participant = conference.getParticipantById(id);
  1149. if (!participant || participant._status === status) {
  1150. return;
  1151. }
  1152. participant._status = status;
  1153. conference.eventEmitter.emit(JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  1154. });
  1155. conference.rtc.addListener(RTCEvents.DOMINANTSPEAKER_CHANGED, function (id) {
  1156. if(conference.lastDominantSpeaker !== id && conference.room) {
  1157. conference.lastDominantSpeaker = id;
  1158. conference.eventEmitter.emit(JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  1159. }
  1160. if (conference.statistics && conference.myUserId() === id) {
  1161. // We are the new dominant speaker.
  1162. conference.statistics.sendDominantSpeakerEvent();
  1163. }
  1164. });
  1165. conference.rtc.addListener(RTCEvents.DATA_CHANNEL_OPEN, function () {
  1166. var now = window.performance.now();
  1167. logger.log("(TIME) data channel opened ", now);
  1168. conference.room.connectionTimes["data.channel.opened"] = now;
  1169. });
  1170. conference.rtc.addListener(RTCEvents.LASTN_CHANGED, function (oldValue, newValue) {
  1171. conference.eventEmitter.emit(JitsiConferenceEvents.IN_LAST_N_CHANGED, oldValue, newValue);
  1172. });
  1173. conference.rtc.addListener(RTCEvents.LASTN_ENDPOINT_CHANGED,
  1174. function (lastNEndpoints, endpointsEnteringLastN) {
  1175. conference.eventEmitter.emit(JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  1176. lastNEndpoints, endpointsEnteringLastN);
  1177. });
  1178. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  1179. function (audioMuted, videoMuted) {
  1180. conference.startAudioMuted = audioMuted;
  1181. conference.startVideoMuted = videoMuted;
  1182. // mute existing local tracks because this is initial mute from
  1183. // Jicofo
  1184. conference.getLocalTracks().forEach(function (track) {
  1185. if (conference.startAudioMuted && track.isAudioTrack()) {
  1186. track.mute();
  1187. }
  1188. if (conference.startVideoMuted && track.isVideoTrack()) {
  1189. track.mute();
  1190. }
  1191. });
  1192. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  1193. });
  1194. conference.room.addPresenceListener("startmuted", function (data, from) {
  1195. var isModerator = false;
  1196. if (conference.myUserId() === from && conference.isModerator()) {
  1197. isModerator = true;
  1198. } else {
  1199. var participant = conference.getParticipantById(from);
  1200. if (participant && participant.isModerator()) {
  1201. isModerator = true;
  1202. }
  1203. }
  1204. if (!isModerator) {
  1205. return;
  1206. }
  1207. var startAudioMuted = data.attributes.audio === 'true';
  1208. var startVideoMuted = data.attributes.video === 'true';
  1209. var updated = false;
  1210. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  1211. conference.startMutedPolicy.audio = startAudioMuted;
  1212. updated = true;
  1213. }
  1214. if (startVideoMuted !== conference.startMutedPolicy.video) {
  1215. conference.startMutedPolicy.video = startVideoMuted;
  1216. updated = true;
  1217. }
  1218. if (updated) {
  1219. conference.eventEmitter.emit(
  1220. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  1221. conference.startMutedPolicy
  1222. );
  1223. }
  1224. });
  1225. conference.rtc.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  1226. conference.room.updateDeviceAvailability(devices);
  1227. });
  1228. conference.room.addPresenceListener("devices", function (data, from) {
  1229. var isAudioAvailable = false;
  1230. var isVideoAvailable = false;
  1231. data.children.forEach(function (config) {
  1232. if (config.tagName === 'audio') {
  1233. isAudioAvailable = config.value === 'true';
  1234. }
  1235. if (config.tagName === 'video') {
  1236. isVideoAvailable = config.value === 'true';
  1237. }
  1238. });
  1239. var availableDevices;
  1240. if (conference.myUserId() === from) {
  1241. availableDevices = conference.availableDevices;
  1242. } else {
  1243. var participant = conference.getParticipantById(from);
  1244. if (!participant) {
  1245. return;
  1246. }
  1247. availableDevices = participant._availableDevices;
  1248. }
  1249. var updated = false;
  1250. if (availableDevices.audio !== isAudioAvailable) {
  1251. updated = true;
  1252. availableDevices.audio = isAudioAvailable;
  1253. }
  1254. if (availableDevices.video !== isVideoAvailable) {
  1255. updated = true;
  1256. availableDevices.video = isVideoAvailable;
  1257. }
  1258. if (updated) {
  1259. conference.eventEmitter.emit(
  1260. JitsiConferenceEvents.AVAILABLE_DEVICES_CHANGED,
  1261. from, availableDevices);
  1262. }
  1263. });
  1264. if(conference.statistics) {
  1265. //FIXME: Maybe remove event should not be associated with the conference.
  1266. conference.statistics.addAudioLevelListener(function (ssrc, level) {
  1267. var userId = null;
  1268. var resource = conference.rtc.getResourceBySSRC(ssrc);
  1269. if (!resource)
  1270. return;
  1271. conference.rtc.setAudioLevel(resource, level);
  1272. });
  1273. conference.statistics.addAudioProblemListener(function (ssrc) {
  1274. conference._reportAudioProblem(ssrc)
  1275. });
  1276. conference.statistics.addConnectionStatsListener(function (stats) {
  1277. var ssrc2resolution = stats.resolution;
  1278. var id2resolution = {};
  1279. // preprocess resolutions: group by user id, skip incorrect
  1280. // resolutions etc.
  1281. Object.keys(ssrc2resolution).forEach(function (ssrc) {
  1282. var resolution = ssrc2resolution[ssrc];
  1283. if (!resolution.width || !resolution.height ||
  1284. resolution.width == -1 || resolution.height == -1) {
  1285. return;
  1286. }
  1287. var id = conference.rtc.getResourceBySSRC(ssrc);
  1288. if (!id) {
  1289. return;
  1290. }
  1291. // ssrc to resolution map for user id
  1292. var idResolutions = id2resolution[id] || {};
  1293. idResolutions[ssrc] = resolution;
  1294. id2resolution[id] = idResolutions;
  1295. });
  1296. stats.resolution = id2resolution;
  1297. conference.eventEmitter.emit(
  1298. JitsiConferenceEvents.CONNECTION_STATS, stats);
  1299. });
  1300. conference.room.addListener(XMPPEvents.DISPOSE_CONFERENCE,
  1301. function () {
  1302. conference.statistics.dispose();
  1303. });
  1304. conference.room.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
  1305. function (pc) {
  1306. conference.statistics.sendIceConnectionFailedEvent(pc);
  1307. conference.room.eventEmitter.emit(
  1308. XMPPEvents.CONFERENCE_SETUP_FAILED,
  1309. new Error("ICE fail"));
  1310. });
  1311. conference.rtc.addListener(RTCEvents.TRACK_ATTACHED,
  1312. function(track, container) {
  1313. var ssrc = track.getSSRC();
  1314. if (!container.id || !ssrc) {
  1315. return;
  1316. }
  1317. conference.statistics.associateStreamWithVideoTag(
  1318. ssrc, track.isLocal(), track.getUsageLabel(), container.id);
  1319. });
  1320. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  1321. function (track) {
  1322. if(!track.isLocal())
  1323. return;
  1324. var type = (track.getType() === "audio")? "audio" : "video";
  1325. conference.statistics.sendMuteEvent(track.isMuted(), type);
  1326. });
  1327. conference.room.addListener(XMPPEvents.CREATE_OFFER_FAILED, function (e, pc) {
  1328. conference.statistics.sendCreateOfferFailed(e, pc);
  1329. });
  1330. conference.room.addListener(XMPPEvents.CREATE_ANSWER_FAILED, function (e, pc) {
  1331. conference.statistics.sendCreateAnswerFailed(e, pc);
  1332. });
  1333. conference.room.addListener(XMPPEvents.SET_LOCAL_DESCRIPTION_FAILED,
  1334. function (e, pc) {
  1335. conference.statistics.sendSetLocalDescFailed(e, pc);
  1336. }
  1337. );
  1338. conference.room.addListener(XMPPEvents.SET_REMOTE_DESCRIPTION_FAILED,
  1339. function (e, pc) {
  1340. conference.statistics.sendSetRemoteDescFailed(e, pc);
  1341. }
  1342. );
  1343. conference.room.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  1344. function (e, pc) {
  1345. conference.statistics.sendAddIceCandidateFailed(e, pc);
  1346. }
  1347. );
  1348. }
  1349. }
  1350. module.exports = JitsiConference;