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

JitsiConference.js 48KB

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