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 25KB

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