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.

JitsiConferenceEventManager.js 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /* global __filename */
  2. import { Strophe } from 'strophe.js';
  3. import {
  4. ACTION_JINGLE_SA_TIMEOUT,
  5. createBridgeDownEvent,
  6. createConnectionStageReachedEvent,
  7. createFocusLeftEvent,
  8. createJingleEvent,
  9. createRemotelyMutedEvent
  10. } from './service/statistics/AnalyticsEvents';
  11. import AuthenticationEvents
  12. from './service/authentication/AuthenticationEvents';
  13. import EventEmitterForwarder from './modules/util/EventEmitterForwarder';
  14. import { getLogger } from 'jitsi-meet-logger';
  15. import * as JitsiConferenceErrors from './JitsiConferenceErrors';
  16. import * as JitsiConferenceEvents from './JitsiConferenceEvents';
  17. import * as MediaType from './service/RTC/MediaType';
  18. import RTCEvents from './service/RTC/RTCEvents';
  19. import VideoType from './service/RTC/VideoType';
  20. import Statistics from './modules/statistics/statistics';
  21. import XMPPEvents from './service/xmpp/XMPPEvents';
  22. const logger = getLogger(__filename);
  23. /**
  24. * Setups all event listeners related to conference
  25. * @param conference {JitsiConference} the conference
  26. */
  27. export default function JitsiConferenceEventManager(conference) {
  28. this.conference = conference;
  29. this.xmppListeners = {};
  30. // Listeners related to the conference only
  31. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  32. track => {
  33. if (!track.isLocal() || !conference.statistics) {
  34. return;
  35. }
  36. const session
  37. = track.isP2P
  38. ? conference.p2pJingleSession : conference.jvbJingleSession;
  39. // TPC will be null, before the conference starts, but the event
  40. // still should be queued
  41. const tpc = (session && session.peerconnection) || null;
  42. conference.statistics.sendMuteEvent(
  43. tpc,
  44. track.isMuted(),
  45. track.getType());
  46. });
  47. }
  48. /**
  49. * Setups event listeners related to conference.chatRoom
  50. */
  51. JitsiConferenceEventManager.prototype.setupChatRoomListeners = function() {
  52. const conference = this.conference;
  53. const chatRoom = conference.room;
  54. this.chatRoomForwarder = new EventEmitterForwarder(chatRoom,
  55. this.conference.eventEmitter);
  56. chatRoom.addListener(XMPPEvents.ICE_RESTARTING, jingleSession => {
  57. if (!jingleSession.isP2P) {
  58. // If using DataChannel as bridge channel, it must be closed
  59. // before ICE restart, otherwise Chrome will not trigger "opened"
  60. // event for the channel established with the new bridge.
  61. // TODO: This may be bypassed when using a WebSocket as bridge
  62. // channel.
  63. conference.rtc.closeBridgeChannel();
  64. }
  65. // else: there are no DataChannels in P2P session (at least for now)
  66. });
  67. chatRoom.addListener(
  68. XMPPEvents.ICE_RESTART_SUCCESS,
  69. (jingleSession, offerIq) => {
  70. // The JVB data chanel needs to be reopened in case the conference
  71. // has been moved to a new bridge.
  72. !jingleSession.isP2P
  73. && conference._setBridgeChannel(
  74. offerIq, jingleSession.peerconnection);
  75. });
  76. chatRoom.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  77. actor => {
  78. // TODO: Add a way to differentiate between commands which caused
  79. // us to mute and those that did not change our state (i.e. we were
  80. // already muted).
  81. Statistics.sendAnalytics(createRemotelyMutedEvent());
  82. conference.mutedByFocusActor = actor;
  83. // set isMutedByFocus when setAudioMute Promise ends
  84. conference.rtc.setAudioMute(true).then(
  85. () => {
  86. conference.isMutedByFocus = true;
  87. conference.mutedByFocusActor = null;
  88. })
  89. .catch(
  90. error => {
  91. conference.mutedByFocusActor = null;
  92. logger.warn(
  93. 'Error while audio muting due to focus request', error);
  94. });
  95. }
  96. );
  97. this.chatRoomForwarder.forward(XMPPEvents.SUBJECT_CHANGED,
  98. JitsiConferenceEvents.SUBJECT_CHANGED);
  99. this.chatRoomForwarder.forward(XMPPEvents.MUC_JOINED,
  100. JitsiConferenceEvents.CONFERENCE_JOINED);
  101. // send some analytics events
  102. chatRoom.addListener(XMPPEvents.MUC_JOINED,
  103. () => {
  104. this.conference.isJvbConnectionInterrupted = false;
  105. // TODO: Move all of the 'connectionTimes' logic to its own module.
  106. Object.keys(chatRoom.connectionTimes).forEach(key => {
  107. const event
  108. = createConnectionStageReachedEvent(
  109. `conference_${key}`,
  110. { value: chatRoom.connectionTimes[key] });
  111. Statistics.sendAnalytics(event);
  112. });
  113. // TODO: Move all of the 'connectionTimes' logic to its own module.
  114. Object.keys(chatRoom.xmpp.connectionTimes).forEach(key => {
  115. const event
  116. = createConnectionStageReachedEvent(
  117. `xmpp_${key}`,
  118. { value: chatRoom.xmpp.connectionTimes[key] });
  119. Statistics.sendAnalytics(event);
  120. });
  121. });
  122. chatRoom.addListener(XMPPEvents.RENEGOTIATION_FAILED, (e, session) => {
  123. if (!session.isP2P) {
  124. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  125. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  126. }
  127. });
  128. this.chatRoomForwarder.forward(XMPPEvents.ROOM_JOIN_ERROR,
  129. JitsiConferenceEvents.CONFERENCE_FAILED,
  130. JitsiConferenceErrors.CONNECTION_ERROR);
  131. this.chatRoomForwarder.forward(XMPPEvents.ROOM_CONNECT_ERROR,
  132. JitsiConferenceEvents.CONFERENCE_FAILED,
  133. JitsiConferenceErrors.CONNECTION_ERROR);
  134. this.chatRoomForwarder.forward(XMPPEvents.ROOM_CONNECT_NOT_ALLOWED_ERROR,
  135. JitsiConferenceEvents.CONFERENCE_FAILED,
  136. JitsiConferenceErrors.NOT_ALLOWED_ERROR);
  137. this.chatRoomForwarder.forward(XMPPEvents.ROOM_MAX_USERS_ERROR,
  138. JitsiConferenceEvents.CONFERENCE_FAILED,
  139. JitsiConferenceErrors.CONFERENCE_MAX_USERS);
  140. this.chatRoomForwarder.forward(XMPPEvents.PASSWORD_REQUIRED,
  141. JitsiConferenceEvents.CONFERENCE_FAILED,
  142. JitsiConferenceErrors.PASSWORD_REQUIRED);
  143. this.chatRoomForwarder.forward(XMPPEvents.AUTHENTICATION_REQUIRED,
  144. JitsiConferenceEvents.CONFERENCE_FAILED,
  145. JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  146. this.chatRoomForwarder.forward(XMPPEvents.BRIDGE_DOWN,
  147. JitsiConferenceEvents.CONFERENCE_FAILED,
  148. JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  149. chatRoom.addListener(
  150. XMPPEvents.BRIDGE_DOWN,
  151. () => Statistics.sendAnalytics(createBridgeDownEvent()));
  152. this.chatRoomForwarder.forward(XMPPEvents.RESERVATION_ERROR,
  153. JitsiConferenceEvents.CONFERENCE_FAILED,
  154. JitsiConferenceErrors.RESERVATION_ERROR);
  155. this.chatRoomForwarder.forward(XMPPEvents.GRACEFUL_SHUTDOWN,
  156. JitsiConferenceEvents.CONFERENCE_FAILED,
  157. JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  158. chatRoom.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
  159. jingleSession => {
  160. conference._onIceConnectionFailed(jingleSession);
  161. });
  162. this.chatRoomForwarder.forward(XMPPEvents.MUC_DESTROYED,
  163. JitsiConferenceEvents.CONFERENCE_FAILED,
  164. JitsiConferenceErrors.CONFERENCE_DESTROYED);
  165. this.chatRoomForwarder.forward(XMPPEvents.CHAT_ERROR_RECEIVED,
  166. JitsiConferenceEvents.CONFERENCE_ERROR,
  167. JitsiConferenceErrors.CHAT_ERROR);
  168. this.chatRoomForwarder.forward(XMPPEvents.FOCUS_DISCONNECTED,
  169. JitsiConferenceEvents.CONFERENCE_FAILED,
  170. JitsiConferenceErrors.FOCUS_DISCONNECTED);
  171. chatRoom.addListener(XMPPEvents.FOCUS_LEFT,
  172. () => {
  173. Statistics.sendAnalytics(createFocusLeftEvent());
  174. conference.eventEmitter.emit(
  175. JitsiConferenceEvents.CONFERENCE_FAILED,
  176. JitsiConferenceErrors.FOCUS_LEFT);
  177. });
  178. chatRoom.addListener(XMPPEvents.SESSION_ACCEPT_TIMEOUT,
  179. jingleSession => {
  180. Statistics.sendAnalyticsAndLog(
  181. createJingleEvent(
  182. ACTION_JINGLE_SA_TIMEOUT,
  183. { p2p: jingleSession.isP2P }));
  184. });
  185. chatRoom.addListener(XMPPEvents.RECORDER_STATE_CHANGED,
  186. (session, jid) => {
  187. if (jid) {
  188. const participant = conference.getParticipantById(
  189. Strophe.getResourceFromJid(jid));
  190. if (session.getStatus() === 'off') {
  191. session.setTerminator(participant);
  192. } else if (session.getStatus() === 'on') {
  193. session.setInitiator(participant);
  194. }
  195. }
  196. conference.eventEmitter.emit(
  197. JitsiConferenceEvents.RECORDER_STATE_CHANGED,
  198. session);
  199. });
  200. this.chatRoomForwarder.forward(XMPPEvents.TRANSCRIPTION_STATUS_CHANGED,
  201. JitsiConferenceEvents.TRANSCRIPTION_STATUS_CHANGED);
  202. this.chatRoomForwarder.forward(XMPPEvents.VIDEO_SIP_GW_AVAILABILITY_CHANGED,
  203. JitsiConferenceEvents.VIDEO_SIP_GW_AVAILABILITY_CHANGED);
  204. this.chatRoomForwarder.forward(
  205. XMPPEvents.VIDEO_SIP_GW_SESSION_STATE_CHANGED,
  206. JitsiConferenceEvents.VIDEO_SIP_GW_SESSION_STATE_CHANGED);
  207. this.chatRoomForwarder.forward(XMPPEvents.PHONE_NUMBER_CHANGED,
  208. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  209. chatRoom.setParticipantPropertyListener((node, from) => {
  210. const participant = conference.getParticipantById(from);
  211. if (!participant) {
  212. return;
  213. }
  214. participant.setProperty(
  215. node.tagName.substring('jitsi_participant_'.length),
  216. node.value);
  217. });
  218. chatRoom.addListener(XMPPEvents.KICKED,
  219. conference.onMemberKicked.bind(conference));
  220. chatRoom.addListener(XMPPEvents.SUSPEND_DETECTED,
  221. conference.onSuspendDetected.bind(conference));
  222. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOCK_CHANGED,
  223. JitsiConferenceEvents.LOCK_STATE_CHANGED);
  224. chatRoom.addListener(XMPPEvents.MUC_MEMBER_JOINED,
  225. conference.onMemberJoined.bind(conference));
  226. chatRoom.addListener(XMPPEvents.MUC_MEMBER_BOT_TYPE_CHANGED,
  227. conference._onMemberBotTypeChanged.bind(conference));
  228. chatRoom.addListener(XMPPEvents.MUC_MEMBER_LEFT,
  229. conference.onMemberLeft.bind(conference));
  230. this.chatRoomForwarder.forward(XMPPEvents.MUC_LEFT,
  231. JitsiConferenceEvents.CONFERENCE_LEFT);
  232. chatRoom.addListener(XMPPEvents.DISPLAY_NAME_CHANGED,
  233. conference.onDisplayNameChanged.bind(conference));
  234. chatRoom.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, role => {
  235. conference.onLocalRoleChanged(role);
  236. // log all events for the recorder operated by the moderator
  237. if (conference.statistics && conference.isModerator()) {
  238. conference.on(JitsiConferenceEvents.RECORDER_STATE_CHANGED,
  239. recorderSession => {
  240. const logObject = {
  241. error: recorderSession.getError(),
  242. id: 'recorder_status',
  243. status: recorderSession.getStatus()
  244. };
  245. Statistics.sendLog(JSON.stringify(logObject));
  246. });
  247. }
  248. });
  249. chatRoom.addListener(XMPPEvents.MUC_ROLE_CHANGED,
  250. conference.onUserRoleChanged.bind(conference));
  251. chatRoom.addListener(AuthenticationEvents.IDENTITY_UPDATED,
  252. (authEnabled, authIdentity) => {
  253. conference.authEnabled = authEnabled;
  254. conference.authIdentity = authIdentity;
  255. conference.eventEmitter.emit(
  256. JitsiConferenceEvents.AUTH_STATUS_CHANGED, authEnabled,
  257. authIdentity);
  258. });
  259. chatRoom.addListener(
  260. XMPPEvents.MESSAGE_RECEIVED,
  261. // eslint-disable-next-line max-params
  262. (jid, displayName, txt, myJid, ts) => {
  263. const id = Strophe.getResourceFromJid(jid);
  264. conference.eventEmitter.emit(
  265. JitsiConferenceEvents.MESSAGE_RECEIVED,
  266. id, txt, ts, displayName);
  267. });
  268. chatRoom.addListener(
  269. XMPPEvents.PRIVATE_MESSAGE_RECEIVED,
  270. // eslint-disable-next-line max-params
  271. (jid, displayName, txt, myJid, ts) => {
  272. const id = Strophe.getResourceFromJid(jid);
  273. conference.eventEmitter.emit(
  274. JitsiConferenceEvents.PRIVATE_MESSAGE_RECEIVED,
  275. id, txt, ts);
  276. });
  277. chatRoom.addListener(XMPPEvents.PRESENCE_STATUS,
  278. (jid, status) => {
  279. const id = Strophe.getResourceFromJid(jid);
  280. const participant = conference.getParticipantById(id);
  281. if (!participant || participant._status === status) {
  282. return;
  283. }
  284. participant._status = status;
  285. conference.eventEmitter.emit(
  286. JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  287. });
  288. chatRoom.addListener(XMPPEvents.JSON_MESSAGE_RECEIVED,
  289. (from, payload) => {
  290. const id = Strophe.getResourceFromJid(from);
  291. const participant = conference.getParticipantById(id);
  292. if (participant) {
  293. conference.eventEmitter.emit(
  294. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  295. participant, payload);
  296. } else {
  297. logger.warn(
  298. 'Ignored XMPPEvents.JSON_MESSAGE_RECEIVED for not existing '
  299. + `participant: ${from}`,
  300. payload);
  301. }
  302. });
  303. chatRoom.addPresenceListener('startmuted', (data, from) => {
  304. let isModerator = false;
  305. if (conference.myUserId() === from && conference.isModerator()) {
  306. isModerator = true;
  307. } else {
  308. const participant = conference.getParticipantById(from);
  309. if (participant && participant.isModerator()) {
  310. isModerator = true;
  311. }
  312. }
  313. if (!isModerator) {
  314. return;
  315. }
  316. const startAudioMuted = data.attributes.audio === 'true';
  317. const startVideoMuted = data.attributes.video === 'true';
  318. let updated = false;
  319. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  320. conference.startMutedPolicy.audio = startAudioMuted;
  321. updated = true;
  322. }
  323. if (startVideoMuted !== conference.startMutedPolicy.video) {
  324. conference.startMutedPolicy.video = startVideoMuted;
  325. updated = true;
  326. }
  327. if (updated) {
  328. conference.eventEmitter.emit(
  329. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  330. conference.startMutedPolicy
  331. );
  332. }
  333. });
  334. if (conference.statistics) {
  335. // FIXME ICE related events should end up in RTCEvents eventually
  336. chatRoom.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
  337. session => {
  338. conference.statistics.sendIceConnectionFailedEvent(
  339. session.peerconnection);
  340. });
  341. // FIXME XMPPEvents.ADD_ICE_CANDIDATE_FAILED is never emitted
  342. chatRoom.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  343. (e, pc) => {
  344. conference.statistics.sendAddIceCandidateFailed(e, pc);
  345. });
  346. }
  347. };
  348. /**
  349. * Setups event listeners related to conference.rtc
  350. */
  351. JitsiConferenceEventManager.prototype.setupRTCListeners = function() {
  352. const conference = this.conference;
  353. const rtc = conference.rtc;
  354. rtc.addListener(
  355. RTCEvents.REMOTE_TRACK_ADDED,
  356. conference.onRemoteTrackAdded.bind(conference));
  357. rtc.addListener(
  358. RTCEvents.REMOTE_TRACK_REMOVED,
  359. conference.onRemoteTrackRemoved.bind(conference));
  360. rtc.addListener(RTCEvents.DOMINANT_SPEAKER_CHANGED,
  361. id => {
  362. if (conference.lastDominantSpeaker !== id && conference.room) {
  363. conference.lastDominantSpeaker = id;
  364. conference.eventEmitter.emit(
  365. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  366. if (conference.statistics && conference.myUserId() === id) {
  367. // We are the new dominant speaker.
  368. conference.statistics.sendDominantSpeakerEvent(
  369. conference.room.roomjid);
  370. }
  371. }
  372. });
  373. rtc.addListener(RTCEvents.DATA_CHANNEL_OPEN, () => {
  374. const now = window.performance.now();
  375. const key = 'data.channel.opened';
  376. // TODO: Move all of the 'connectionTimes' logic to its own module.
  377. logger.log(`(TIME) ${key}`, now);
  378. conference.room.connectionTimes[key] = now;
  379. Statistics.sendAnalytics(
  380. createConnectionStageReachedEvent(key, { value: now }));
  381. conference.eventEmitter.emit(JitsiConferenceEvents.DATA_CHANNEL_OPENED);
  382. });
  383. rtc.addListener(RTCEvents.ENDPOINT_MESSAGE_RECEIVED,
  384. (from, payload) => {
  385. const participant = conference.getParticipantById(from);
  386. if (participant) {
  387. conference.eventEmitter.emit(
  388. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  389. participant, payload);
  390. } else {
  391. logger.warn(
  392. 'Ignored ENDPOINT_MESSAGE_RECEIVED for not existing '
  393. + `participant: ${from}`,
  394. payload);
  395. }
  396. });
  397. rtc.addListener(RTCEvents.LOCAL_UFRAG_CHANGED,
  398. (tpc, ufrag) => {
  399. if (!tpc.isP2P) {
  400. Statistics.sendLog(
  401. JSON.stringify({
  402. id: 'local_ufrag',
  403. value: ufrag
  404. }));
  405. }
  406. });
  407. rtc.addListener(RTCEvents.REMOTE_UFRAG_CHANGED,
  408. (tpc, ufrag) => {
  409. if (!tpc.isP2P) {
  410. Statistics.sendLog(
  411. JSON.stringify({
  412. id: 'remote_ufrag',
  413. value: ufrag
  414. }));
  415. }
  416. });
  417. rtc.addListener(RTCEvents.CREATE_ANSWER_FAILED,
  418. (e, tpc) => {
  419. conference.statistics.sendCreateAnswerFailed(e, tpc);
  420. if (!tpc.isP2P) {
  421. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  422. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  423. }
  424. });
  425. rtc.addListener(RTCEvents.CREATE_OFFER_FAILED,
  426. (e, tpc) => {
  427. conference.statistics.sendCreateOfferFailed(e, tpc);
  428. if (!tpc.isP2P) {
  429. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  430. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  431. }
  432. });
  433. rtc.addListener(RTCEvents.SET_LOCAL_DESCRIPTION_FAILED,
  434. (e, tpc) => {
  435. conference.statistics.sendSetLocalDescFailed(e, tpc);
  436. if (!tpc.isP2P) {
  437. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  438. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  439. }
  440. });
  441. rtc.addListener(RTCEvents.SET_REMOTE_DESCRIPTION_FAILED,
  442. (e, tpc) => {
  443. conference.statistics.sendSetRemoteDescFailed(e, tpc);
  444. if (!tpc.isP2P) {
  445. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  446. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  447. }
  448. });
  449. rtc.addListener(RTCEvents.LOCAL_TRACK_SSRC_UPDATED,
  450. (track, ssrc) => {
  451. // when starting screen sharing, the track is created and when
  452. // we do set local description and we process the ssrc we
  453. // will be notified for it and we will report it with the event
  454. // for screen sharing
  455. if (track.isVideoTrack() && track.videoType === VideoType.DESKTOP) {
  456. conference.statistics.sendScreenSharingEvent(true, ssrc);
  457. }
  458. });
  459. };
  460. /**
  461. * Removes event listeners related to conference.xmpp
  462. */
  463. JitsiConferenceEventManager.prototype.removeXMPPListeners = function() {
  464. const conference = this.conference;
  465. conference.xmpp.caps.removeListener(
  466. XMPPEvents.PARTCIPANT_FEATURES_CHANGED,
  467. this.xmppListeners[XMPPEvents.PARTCIPANT_FEATURES_CHANGED]);
  468. delete this.xmppListeners[XMPPEvents.PARTCIPANT_FEATURES_CHANGED];
  469. Object.keys(this.xmppListeners).forEach(eventName => {
  470. conference.xmpp.removeListener(
  471. eventName,
  472. this.xmppListeners[eventName]);
  473. });
  474. this.xmppListeners = {};
  475. };
  476. /**
  477. * Setups event listeners related to conference.xmpp
  478. */
  479. JitsiConferenceEventManager.prototype.setupXMPPListeners = function() {
  480. const conference = this.conference;
  481. const featuresChangedListener = from => {
  482. const participant
  483. = conference.getParticipantById(
  484. Strophe.getResourceFromJid(from));
  485. if (participant) {
  486. conference.eventEmitter.emit(
  487. JitsiConferenceEvents.PARTCIPANT_FEATURES_CHANGED,
  488. participant);
  489. }
  490. };
  491. conference.xmpp.caps.addListener(
  492. XMPPEvents.PARTCIPANT_FEATURES_CHANGED,
  493. featuresChangedListener);
  494. this.xmppListeners[XMPPEvents.PARTCIPANT_FEATURES_CHANGED]
  495. = featuresChangedListener;
  496. this._addConferenceXMPPListener(
  497. XMPPEvents.CALL_INCOMING,
  498. conference.onIncomingCall.bind(conference));
  499. this._addConferenceXMPPListener(
  500. XMPPEvents.CALL_ACCEPTED,
  501. conference.onCallAccepted.bind(conference));
  502. this._addConferenceXMPPListener(
  503. XMPPEvents.TRANSPORT_INFO,
  504. conference.onTransportInfo.bind(conference));
  505. this._addConferenceXMPPListener(
  506. XMPPEvents.CALL_ENDED,
  507. conference.onCallEnded.bind(conference));
  508. this._addConferenceXMPPListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  509. (audioMuted, videoMuted) => {
  510. if (conference.options.config.ignoreStartMuted) {
  511. return;
  512. }
  513. conference.startAudioMuted = audioMuted;
  514. conference.startVideoMuted = videoMuted;
  515. // mute existing local tracks because this is initial mute from
  516. // Jicofo
  517. conference.getLocalTracks().forEach(track => {
  518. switch (track.getType()) {
  519. case MediaType.AUDIO:
  520. conference.startAudioMuted && track.mute();
  521. break;
  522. case MediaType.VIDEO:
  523. conference.startVideoMuted && track.mute();
  524. break;
  525. }
  526. });
  527. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  528. });
  529. this._addConferenceXMPPListener(XMPPEvents.CONFERENCE_TIMESTAMP_RECEIVED,
  530. createdTimestamp => {
  531. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_CREATED_TIMESTAMP, createdTimestamp);
  532. });
  533. };
  534. /**
  535. * Add XMPP listener and save its reference for remove on leave conference.
  536. */
  537. JitsiConferenceEventManager.prototype._addConferenceXMPPListener = function(
  538. eventName, listener) {
  539. this.xmppListeners[eventName] = listener;
  540. this.conference.xmpp.addListener(eventName, listener);
  541. };
  542. /**
  543. * Setups event listeners related to conference.statistics
  544. */
  545. JitsiConferenceEventManager.prototype.setupStatisticsListeners = function() {
  546. const conference = this.conference;
  547. if (!conference.statistics) {
  548. return;
  549. }
  550. /* eslint-disable max-params */
  551. conference.statistics.addAudioLevelListener((tpc, ssrc, level, isLocal) => {
  552. conference.rtc.setAudioLevel(tpc, ssrc, level, isLocal);
  553. });
  554. /* eslint-enable max-params */
  555. // Forward the "before stats disposed" event
  556. conference.statistics.addBeforeDisposedListener(() => {
  557. conference.eventEmitter.emit(
  558. JitsiConferenceEvents.BEFORE_STATISTICS_DISPOSED);
  559. });
  560. // if we are in startSilent mode we will not be sending/receiving so nothing to detect
  561. if (!conference.options.config.startSilent) {
  562. conference.statistics.addByteSentStatsListener((tpc, stats) => {
  563. conference.getLocalTracks(MediaType.AUDIO).forEach(track => {
  564. const ssrc = tpc.getLocalSSRC(track);
  565. if (!ssrc || !stats.hasOwnProperty(ssrc)) {
  566. return;
  567. }
  568. track._onByteSentStatsReceived(tpc, stats[ssrc]);
  569. });
  570. });
  571. }
  572. };