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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. this.chatRoomForwarder.forward(XMPPEvents.RECORDER_STATE_CHANGED,
  186. JitsiConferenceEvents.RECORDER_STATE_CHANGED);
  187. this.chatRoomForwarder.forward(XMPPEvents.TRANSCRIPTION_STATUS_CHANGED,
  188. JitsiConferenceEvents.TRANSCRIPTION_STATUS_CHANGED);
  189. this.chatRoomForwarder.forward(XMPPEvents.VIDEO_SIP_GW_AVAILABILITY_CHANGED,
  190. JitsiConferenceEvents.VIDEO_SIP_GW_AVAILABILITY_CHANGED);
  191. this.chatRoomForwarder.forward(
  192. XMPPEvents.VIDEO_SIP_GW_SESSION_STATE_CHANGED,
  193. JitsiConferenceEvents.VIDEO_SIP_GW_SESSION_STATE_CHANGED);
  194. this.chatRoomForwarder.forward(XMPPEvents.PHONE_NUMBER_CHANGED,
  195. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  196. chatRoom.setParticipantPropertyListener((node, from) => {
  197. const participant = conference.getParticipantById(from);
  198. if (!participant) {
  199. return;
  200. }
  201. participant.setProperty(
  202. node.tagName.substring('jitsi_participant_'.length),
  203. node.value);
  204. });
  205. chatRoom.addListener(XMPPEvents.KICKED,
  206. conference.onMemberKicked.bind(conference));
  207. chatRoom.addListener(XMPPEvents.SUSPEND_DETECTED,
  208. conference.onSuspendDetected.bind(conference));
  209. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOCK_CHANGED,
  210. JitsiConferenceEvents.LOCK_STATE_CHANGED);
  211. chatRoom.addListener(XMPPEvents.MUC_MEMBER_JOINED,
  212. conference.onMemberJoined.bind(conference));
  213. chatRoom.addListener(XMPPEvents.MUC_MEMBER_BOT_TYPE_CHANGED,
  214. conference._onMemberBotTypeChanged.bind(conference));
  215. chatRoom.addListener(XMPPEvents.MUC_MEMBER_LEFT,
  216. conference.onMemberLeft.bind(conference));
  217. this.chatRoomForwarder.forward(XMPPEvents.MUC_LEFT,
  218. JitsiConferenceEvents.CONFERENCE_LEFT);
  219. chatRoom.addListener(XMPPEvents.DISPLAY_NAME_CHANGED,
  220. conference.onDisplayNameChanged.bind(conference));
  221. chatRoom.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, role => {
  222. conference.onLocalRoleChanged(role);
  223. // log all events for the recorder operated by the moderator
  224. if (conference.statistics && conference.isModerator()) {
  225. conference.on(JitsiConferenceEvents.RECORDER_STATE_CHANGED,
  226. recorderSession => {
  227. const logObject = {
  228. error: recorderSession.getError(),
  229. id: 'recorder_status',
  230. status: recorderSession.getStatus()
  231. };
  232. Statistics.sendLog(JSON.stringify(logObject));
  233. });
  234. }
  235. });
  236. chatRoom.addListener(XMPPEvents.MUC_ROLE_CHANGED,
  237. conference.onUserRoleChanged.bind(conference));
  238. chatRoom.addListener(AuthenticationEvents.IDENTITY_UPDATED,
  239. (authEnabled, authIdentity) => {
  240. conference.authEnabled = authEnabled;
  241. conference.authIdentity = authIdentity;
  242. conference.eventEmitter.emit(
  243. JitsiConferenceEvents.AUTH_STATUS_CHANGED, authEnabled,
  244. authIdentity);
  245. });
  246. chatRoom.addListener(
  247. XMPPEvents.MESSAGE_RECEIVED,
  248. // eslint-disable-next-line max-params
  249. (jid, displayName, txt, myJid, ts) => {
  250. const id = Strophe.getResourceFromJid(jid);
  251. conference.eventEmitter.emit(
  252. JitsiConferenceEvents.MESSAGE_RECEIVED,
  253. id, txt, ts, displayName);
  254. });
  255. chatRoom.addListener(
  256. XMPPEvents.PRIVATE_MESSAGE_RECEIVED,
  257. // eslint-disable-next-line max-params
  258. (jid, displayName, txt, myJid, ts) => {
  259. const id = Strophe.getResourceFromJid(jid);
  260. conference.eventEmitter.emit(
  261. JitsiConferenceEvents.PRIVATE_MESSAGE_RECEIVED,
  262. id, txt, ts);
  263. });
  264. chatRoom.addListener(XMPPEvents.PRESENCE_STATUS,
  265. (jid, status) => {
  266. const id = Strophe.getResourceFromJid(jid);
  267. const participant = conference.getParticipantById(id);
  268. if (!participant || participant._status === status) {
  269. return;
  270. }
  271. participant._status = status;
  272. conference.eventEmitter.emit(
  273. JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  274. });
  275. chatRoom.addListener(XMPPEvents.JSON_MESSAGE_RECEIVED,
  276. (from, payload) => {
  277. const id = Strophe.getResourceFromJid(from);
  278. const participant = conference.getParticipantById(id);
  279. if (participant) {
  280. conference.eventEmitter.emit(
  281. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  282. participant, payload);
  283. } else {
  284. logger.warn(
  285. 'Ignored XMPPEvents.JSON_MESSAGE_RECEIVED for not existing '
  286. + `participant: ${from}`,
  287. payload);
  288. }
  289. });
  290. chatRoom.addPresenceListener('startmuted', (data, from) => {
  291. let isModerator = false;
  292. if (conference.myUserId() === from && conference.isModerator()) {
  293. isModerator = true;
  294. } else {
  295. const participant = conference.getParticipantById(from);
  296. if (participant && participant.isModerator()) {
  297. isModerator = true;
  298. }
  299. }
  300. if (!isModerator) {
  301. return;
  302. }
  303. const startAudioMuted = data.attributes.audio === 'true';
  304. const startVideoMuted = data.attributes.video === 'true';
  305. let updated = false;
  306. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  307. conference.startMutedPolicy.audio = startAudioMuted;
  308. updated = true;
  309. }
  310. if (startVideoMuted !== conference.startMutedPolicy.video) {
  311. conference.startMutedPolicy.video = startVideoMuted;
  312. updated = true;
  313. }
  314. if (updated) {
  315. conference.eventEmitter.emit(
  316. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  317. conference.startMutedPolicy
  318. );
  319. }
  320. });
  321. if (conference.statistics) {
  322. // FIXME ICE related events should end up in RTCEvents eventually
  323. chatRoom.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
  324. session => {
  325. conference.statistics.sendIceConnectionFailedEvent(
  326. session.peerconnection);
  327. });
  328. // FIXME XMPPEvents.ADD_ICE_CANDIDATE_FAILED is never emitted
  329. chatRoom.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  330. (e, pc) => {
  331. conference.statistics.sendAddIceCandidateFailed(e, pc);
  332. });
  333. }
  334. };
  335. /**
  336. * Setups event listeners related to conference.rtc
  337. */
  338. JitsiConferenceEventManager.prototype.setupRTCListeners = function() {
  339. const conference = this.conference;
  340. const rtc = conference.rtc;
  341. rtc.addListener(
  342. RTCEvents.REMOTE_TRACK_ADDED,
  343. conference.onRemoteTrackAdded.bind(conference));
  344. rtc.addListener(
  345. RTCEvents.REMOTE_TRACK_REMOVED,
  346. conference.onRemoteTrackRemoved.bind(conference));
  347. rtc.addListener(RTCEvents.DOMINANT_SPEAKER_CHANGED,
  348. id => {
  349. if (conference.lastDominantSpeaker !== id && conference.room) {
  350. conference.lastDominantSpeaker = id;
  351. conference.eventEmitter.emit(
  352. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  353. if (conference.statistics && conference.myUserId() === id) {
  354. // We are the new dominant speaker.
  355. conference.statistics.sendDominantSpeakerEvent(
  356. conference.room.roomjid);
  357. }
  358. }
  359. });
  360. rtc.addListener(RTCEvents.DATA_CHANNEL_OPEN, () => {
  361. const now = window.performance.now();
  362. const key = 'data.channel.opened';
  363. // TODO: Move all of the 'connectionTimes' logic to its own module.
  364. logger.log(`(TIME) ${key}`, now);
  365. conference.room.connectionTimes[key] = now;
  366. Statistics.sendAnalytics(
  367. createConnectionStageReachedEvent(key, { value: now }));
  368. conference.eventEmitter.emit(JitsiConferenceEvents.DATA_CHANNEL_OPENED);
  369. });
  370. rtc.addListener(RTCEvents.ENDPOINT_MESSAGE_RECEIVED,
  371. (from, payload) => {
  372. const participant = conference.getParticipantById(from);
  373. if (participant) {
  374. conference.eventEmitter.emit(
  375. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  376. participant, payload);
  377. } else {
  378. logger.warn(
  379. 'Ignored ENDPOINT_MESSAGE_RECEIVED for not existing '
  380. + `participant: ${from}`,
  381. payload);
  382. }
  383. });
  384. rtc.addListener(RTCEvents.LOCAL_UFRAG_CHANGED,
  385. (tpc, ufrag) => {
  386. if (!tpc.isP2P) {
  387. Statistics.sendLog(
  388. JSON.stringify({
  389. id: 'local_ufrag',
  390. value: ufrag
  391. }));
  392. }
  393. });
  394. rtc.addListener(RTCEvents.REMOTE_UFRAG_CHANGED,
  395. (tpc, ufrag) => {
  396. if (!tpc.isP2P) {
  397. Statistics.sendLog(
  398. JSON.stringify({
  399. id: 'remote_ufrag',
  400. value: ufrag
  401. }));
  402. }
  403. });
  404. rtc.addListener(RTCEvents.CREATE_ANSWER_FAILED,
  405. (e, tpc) => {
  406. conference.statistics.sendCreateAnswerFailed(e, tpc);
  407. if (!tpc.isP2P) {
  408. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  409. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  410. }
  411. });
  412. rtc.addListener(RTCEvents.CREATE_OFFER_FAILED,
  413. (e, tpc) => {
  414. conference.statistics.sendCreateOfferFailed(e, tpc);
  415. if (!tpc.isP2P) {
  416. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  417. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  418. }
  419. });
  420. rtc.addListener(RTCEvents.SET_LOCAL_DESCRIPTION_FAILED,
  421. (e, tpc) => {
  422. conference.statistics.sendSetLocalDescFailed(e, tpc);
  423. if (!tpc.isP2P) {
  424. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  425. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  426. }
  427. });
  428. rtc.addListener(RTCEvents.SET_REMOTE_DESCRIPTION_FAILED,
  429. (e, tpc) => {
  430. conference.statistics.sendSetRemoteDescFailed(e, tpc);
  431. if (!tpc.isP2P) {
  432. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  433. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  434. }
  435. });
  436. rtc.addListener(RTCEvents.LOCAL_TRACK_SSRC_UPDATED,
  437. (track, ssrc) => {
  438. // when starting screen sharing, the track is created and when
  439. // we do set local description and we process the ssrc we
  440. // will be notified for it and we will report it with the event
  441. // for screen sharing
  442. if (track.isVideoTrack() && track.videoType === VideoType.DESKTOP) {
  443. conference.statistics.sendScreenSharingEvent(true, ssrc);
  444. }
  445. });
  446. };
  447. /**
  448. * Removes event listeners related to conference.xmpp
  449. */
  450. JitsiConferenceEventManager.prototype.removeXMPPListeners = function() {
  451. const conference = this.conference;
  452. conference.xmpp.caps.removeListener(
  453. XMPPEvents.PARTCIPANT_FEATURES_CHANGED,
  454. this.xmppListeners[XMPPEvents.PARTCIPANT_FEATURES_CHANGED]);
  455. delete this.xmppListeners[XMPPEvents.PARTCIPANT_FEATURES_CHANGED];
  456. Object.keys(this.xmppListeners).forEach(eventName => {
  457. conference.xmpp.removeListener(
  458. eventName,
  459. this.xmppListeners[eventName]);
  460. });
  461. this.xmppListeners = {};
  462. };
  463. /**
  464. * Setups event listeners related to conference.xmpp
  465. */
  466. JitsiConferenceEventManager.prototype.setupXMPPListeners = function() {
  467. const conference = this.conference;
  468. const featuresChangedListener = from => {
  469. const participant
  470. = conference.getParticipantById(
  471. Strophe.getResourceFromJid(from));
  472. if (participant) {
  473. conference.eventEmitter.emit(
  474. JitsiConferenceEvents.PARTCIPANT_FEATURES_CHANGED,
  475. participant);
  476. }
  477. };
  478. conference.xmpp.caps.addListener(
  479. XMPPEvents.PARTCIPANT_FEATURES_CHANGED,
  480. featuresChangedListener);
  481. this.xmppListeners[XMPPEvents.PARTCIPANT_FEATURES_CHANGED]
  482. = featuresChangedListener;
  483. this._addConferenceXMPPListener(
  484. XMPPEvents.CALL_INCOMING,
  485. conference.onIncomingCall.bind(conference));
  486. this._addConferenceXMPPListener(
  487. XMPPEvents.CALL_ACCEPTED,
  488. conference.onCallAccepted.bind(conference));
  489. this._addConferenceXMPPListener(
  490. XMPPEvents.TRANSPORT_INFO,
  491. conference.onTransportInfo.bind(conference));
  492. this._addConferenceXMPPListener(
  493. XMPPEvents.CALL_ENDED,
  494. conference.onCallEnded.bind(conference));
  495. this._addConferenceXMPPListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  496. (audioMuted, videoMuted) => {
  497. if (conference.options.config.ignoreStartMuted) {
  498. return;
  499. }
  500. conference.startAudioMuted = audioMuted;
  501. conference.startVideoMuted = videoMuted;
  502. // mute existing local tracks because this is initial mute from
  503. // Jicofo
  504. conference.getLocalTracks().forEach(track => {
  505. switch (track.getType()) {
  506. case MediaType.AUDIO:
  507. conference.startAudioMuted && track.mute();
  508. break;
  509. case MediaType.VIDEO:
  510. conference.startVideoMuted && track.mute();
  511. break;
  512. }
  513. });
  514. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  515. });
  516. };
  517. /**
  518. * Add XMPP listener and save its reference for remove on leave conference.
  519. */
  520. JitsiConferenceEventManager.prototype._addConferenceXMPPListener = function(
  521. eventName, listener) {
  522. this.xmppListeners[eventName] = listener;
  523. this.conference.xmpp.addListener(eventName, listener);
  524. };
  525. /**
  526. * Setups event listeners related to conference.statistics
  527. */
  528. JitsiConferenceEventManager.prototype.setupStatisticsListeners = function() {
  529. const conference = this.conference;
  530. if (!conference.statistics) {
  531. return;
  532. }
  533. /* eslint-disable max-params */
  534. conference.statistics.addAudioLevelListener((tpc, ssrc, level, isLocal) => {
  535. conference.rtc.setAudioLevel(tpc, ssrc, level, isLocal);
  536. });
  537. /* eslint-enable max-params */
  538. // Forward the "before stats disposed" event
  539. conference.statistics.addBeforeDisposedListener(() => {
  540. conference.eventEmitter.emit(
  541. JitsiConferenceEvents.BEFORE_STATISTICS_DISPOSED);
  542. });
  543. // if we are in startSilent mode we will not be sending/receiving so nothing to detect
  544. if (!conference.options.config.startSilent) {
  545. conference.statistics.addByteSentStatsListener((tpc, stats) => {
  546. conference.getLocalTracks(MediaType.AUDIO).forEach(track => {
  547. const ssrc = tpc.getLocalSSRC(track);
  548. if (!ssrc || !stats.hasOwnProperty(ssrc)) {
  549. return;
  550. }
  551. track._onByteSentStatsReceived(tpc, stats[ssrc]);
  552. });
  553. });
  554. }
  555. };