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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. import { getLogger } from '@jitsi/logger';
  2. import { Strophe } from 'strophe.js';
  3. import * as JitsiConferenceErrors from './JitsiConferenceErrors';
  4. import * as JitsiConferenceEvents from './JitsiConferenceEvents';
  5. import * as JitsiTrackEvents from './JitsiTrackEvents';
  6. import { SPEAKERS_AUDIO_LEVELS } from './modules/statistics/constants';
  7. import Statistics from './modules/statistics/statistics';
  8. import EventEmitterForwarder from './modules/util/EventEmitterForwarder';
  9. import { MediaType } from './service/RTC/MediaType';
  10. import RTCEvents from './service/RTC/RTCEvents';
  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. }
  31. /**
  32. * Setups event listeners related to conference.chatRoom
  33. */
  34. JitsiConferenceEventManager.prototype.setupChatRoomListeners = function() {
  35. const conference = this.conference;
  36. const chatRoom = conference.room;
  37. this.chatRoomForwarder = new EventEmitterForwarder(chatRoom,
  38. this.conference.eventEmitter);
  39. chatRoom.addListener(XMPPEvents.ICE_RESTARTING, jingleSession => {
  40. if (!jingleSession.isP2P) {
  41. // If using DataChannel as bridge channel, it must be closed
  42. // before ICE restart, otherwise Chrome will not trigger "opened"
  43. // event for the channel established with the new bridge.
  44. // TODO: This may be bypassed when using a WebSocket as bridge
  45. // channel.
  46. conference.rtc.closeBridgeChannel();
  47. }
  48. // else: there are no DataChannels in P2P session (at least for now)
  49. });
  50. chatRoom.addListener(XMPPEvents.PARTICIPANT_FEATURES_CHANGED, (from, features) => {
  51. const participant = conference.getParticipantById(Strophe.getResourceFromJid(from));
  52. if (participant) {
  53. participant.setFeatures(features);
  54. conference.eventEmitter.emit(JitsiConferenceEvents.PARTCIPANT_FEATURES_CHANGED, participant);
  55. }
  56. });
  57. chatRoom.addListener(
  58. XMPPEvents.ICE_RESTART_SUCCESS,
  59. (jingleSession, offerIq) => {
  60. // The JVB data chanel needs to be reopened in case the conference
  61. // has been moved to a new bridge.
  62. !jingleSession.isP2P
  63. && conference._setBridgeChannel(
  64. offerIq, jingleSession.peerconnection);
  65. });
  66. chatRoom.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  67. actor => {
  68. // TODO: Add a way to differentiate between commands which caused
  69. // us to mute and those that did not change our state (i.e. we were
  70. // already muted).
  71. Statistics.sendAnalytics(createRemotelyMutedEvent(MediaType.AUDIO));
  72. conference.mutedByFocusActor = actor;
  73. // set isMutedByFocus when setAudioMute Promise ends
  74. conference.rtc.setAudioMute(true).then(
  75. () => {
  76. conference.isMutedByFocus = true;
  77. conference.mutedByFocusActor = null;
  78. })
  79. .catch(
  80. error => {
  81. conference.mutedByFocusActor = null;
  82. logger.warn(
  83. 'Error while audio muting due to focus request', error);
  84. });
  85. }
  86. );
  87. chatRoom.addListener(XMPPEvents.VIDEO_MUTED_BY_FOCUS,
  88. actor => {
  89. // TODO: Add a way to differentiate between commands which caused
  90. // us to mute and those that did not change our state (i.e. we were
  91. // already muted).
  92. Statistics.sendAnalytics(createRemotelyMutedEvent(MediaType.VIDEO));
  93. conference.mutedVideoByFocusActor = actor;
  94. // set isVideoMutedByFocus when setVideoMute Promise ends
  95. conference.rtc.setVideoMute(true).then(
  96. () => {
  97. conference.isVideoMutedByFocus = true;
  98. conference.mutedVideoByFocusActor = null;
  99. })
  100. .catch(
  101. error => {
  102. conference.mutedVideoByFocusActor = null;
  103. logger.warn(
  104. 'Error while video muting due to focus request', error);
  105. });
  106. }
  107. );
  108. this.chatRoomForwarder.forward(XMPPEvents.SUBJECT_CHANGED,
  109. JitsiConferenceEvents.SUBJECT_CHANGED);
  110. this.chatRoomForwarder.forward(XMPPEvents.MUC_JOINED,
  111. JitsiConferenceEvents.CONFERENCE_JOINED);
  112. this.chatRoomForwarder.forward(XMPPEvents.MUC_JOIN_IN_PROGRESS,
  113. JitsiConferenceEvents.CONFERENCE_JOIN_IN_PROGRESS);
  114. this.chatRoomForwarder.forward(XMPPEvents.MEETING_ID_SET,
  115. JitsiConferenceEvents.CONFERENCE_UNIQUE_ID_SET);
  116. // send some analytics events
  117. chatRoom.addListener(XMPPEvents.MUC_JOINED,
  118. () => {
  119. this.conference._onMucJoined();
  120. this.conference.isJvbConnectionInterrupted = false;
  121. // TODO: Move all of the 'connectionTimes' logic to its own module.
  122. Object.keys(chatRoom.connectionTimes).forEach(key => {
  123. const event
  124. = createConnectionStageReachedEvent(
  125. `conference_${key}`,
  126. { value: chatRoom.connectionTimes[key] });
  127. Statistics.sendAnalytics(event);
  128. });
  129. // TODO: Move all of the 'connectionTimes' logic to its own module.
  130. Object.keys(chatRoom.xmpp.connectionTimes).forEach(key => {
  131. const event
  132. = createConnectionStageReachedEvent(
  133. `xmpp_${key}`,
  134. { value: chatRoom.xmpp.connectionTimes[key] });
  135. Statistics.sendAnalytics(event);
  136. });
  137. });
  138. chatRoom.addListener(XMPPEvents.RENEGOTIATION_FAILED, (e, session) => {
  139. if (!session.isP2P) {
  140. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  141. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  142. }
  143. });
  144. chatRoom.addListener(JitsiTrackEvents.TRACK_REMOVED, track => {
  145. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  146. });
  147. this.chatRoomForwarder.forward(XMPPEvents.ROOM_JOIN_ERROR,
  148. JitsiConferenceEvents.CONFERENCE_FAILED,
  149. JitsiConferenceErrors.CONNECTION_ERROR);
  150. this.chatRoomForwarder.forward(XMPPEvents.DISPLAY_NAME_REQUIRED,
  151. JitsiConferenceEvents.CONFERENCE_FAILED,
  152. JitsiConferenceErrors.DISPLAY_NAME_REQUIRED);
  153. this.chatRoomForwarder.forward(XMPPEvents.ROOM_CONNECT_ERROR,
  154. JitsiConferenceEvents.CONFERENCE_FAILED,
  155. JitsiConferenceErrors.CONNECTION_ERROR);
  156. this.chatRoomForwarder.forward(XMPPEvents.ROOM_CONNECT_NOT_ALLOWED_ERROR,
  157. JitsiConferenceEvents.CONFERENCE_FAILED,
  158. JitsiConferenceErrors.NOT_ALLOWED_ERROR);
  159. this.chatRoomForwarder.forward(XMPPEvents.ROOM_CONNECT_MEMBERS_ONLY_ERROR,
  160. JitsiConferenceEvents.CONFERENCE_FAILED,
  161. JitsiConferenceErrors.MEMBERS_ONLY_ERROR);
  162. this.chatRoomForwarder.forward(XMPPEvents.ROOM_MAX_USERS_ERROR,
  163. JitsiConferenceEvents.CONFERENCE_FAILED,
  164. JitsiConferenceErrors.CONFERENCE_MAX_USERS);
  165. this.chatRoomForwarder.forward(XMPPEvents.PASSWORD_REQUIRED,
  166. JitsiConferenceEvents.CONFERENCE_FAILED,
  167. JitsiConferenceErrors.PASSWORD_REQUIRED);
  168. this.chatRoomForwarder.forward(XMPPEvents.AUTHENTICATION_REQUIRED,
  169. JitsiConferenceEvents.CONFERENCE_FAILED,
  170. JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  171. this.chatRoomForwarder.forward(XMPPEvents.BRIDGE_DOWN,
  172. JitsiConferenceEvents.CONFERENCE_FAILED,
  173. JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  174. chatRoom.addListener(
  175. XMPPEvents.BRIDGE_DOWN,
  176. () => Statistics.sendAnalytics(createBridgeDownEvent()));
  177. chatRoom.addListener(XMPPEvents.CONNECTION_RESTARTED,
  178. jingleSession => {
  179. conference._onConferenceRestarted(jingleSession);
  180. });
  181. this.chatRoomForwarder.forward(XMPPEvents.RESERVATION_ERROR,
  182. JitsiConferenceEvents.CONFERENCE_FAILED,
  183. JitsiConferenceErrors.RESERVATION_ERROR);
  184. this.chatRoomForwarder.forward(XMPPEvents.GRACEFUL_SHUTDOWN,
  185. JitsiConferenceEvents.CONFERENCE_FAILED,
  186. JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  187. chatRoom.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
  188. jingleSession => {
  189. conference._onIceConnectionFailed(jingleSession);
  190. });
  191. this.chatRoomForwarder.forward(XMPPEvents.MUC_DESTROYED,
  192. JitsiConferenceEvents.CONFERENCE_FAILED,
  193. JitsiConferenceErrors.CONFERENCE_DESTROYED);
  194. this.chatRoomForwarder.forward(XMPPEvents.CHAT_ERROR_RECEIVED,
  195. JitsiConferenceEvents.CONFERENCE_ERROR,
  196. JitsiConferenceErrors.CHAT_ERROR);
  197. this.chatRoomForwarder.forward(XMPPEvents.SETTINGS_ERROR_RECEIVED,
  198. JitsiConferenceEvents.CONFERENCE_ERROR,
  199. JitsiConferenceErrors.SETTINGS_ERROR);
  200. this.chatRoomForwarder.forward(XMPPEvents.FOCUS_DISCONNECTED,
  201. JitsiConferenceEvents.CONFERENCE_FAILED,
  202. JitsiConferenceErrors.FOCUS_DISCONNECTED);
  203. chatRoom.addListener(XMPPEvents.FOCUS_LEFT,
  204. () => {
  205. Statistics.sendAnalytics(createFocusLeftEvent());
  206. conference.eventEmitter.emit(
  207. JitsiConferenceEvents.CONFERENCE_FAILED,
  208. JitsiConferenceErrors.FOCUS_LEFT);
  209. });
  210. chatRoom.addListener(XMPPEvents.SESSION_ACCEPT_TIMEOUT,
  211. jingleSession => {
  212. Statistics.sendAnalyticsAndLog(
  213. createJingleEvent(
  214. ACTION_JINGLE_SA_TIMEOUT,
  215. { p2p: jingleSession.isP2P }));
  216. });
  217. chatRoom.addListener(XMPPEvents.RECORDER_STATE_CHANGED,
  218. (session, jid) => {
  219. if (jid) {
  220. const resource = Strophe.getResourceFromJid(jid);
  221. const participant = conference.getParticipantById(resource) || resource;
  222. if (session.getStatus() === 'off') {
  223. session.setTerminator(participant);
  224. } else if (session.getStatus() === 'on') {
  225. session.setInitiator(participant);
  226. }
  227. }
  228. conference.eventEmitter.emit(
  229. JitsiConferenceEvents.RECORDER_STATE_CHANGED,
  230. session);
  231. });
  232. this.chatRoomForwarder.forward(XMPPEvents.TRANSCRIPTION_STATUS_CHANGED,
  233. JitsiConferenceEvents.TRANSCRIPTION_STATUS_CHANGED);
  234. this.chatRoomForwarder.forward(XMPPEvents.VIDEO_SIP_GW_AVAILABILITY_CHANGED,
  235. JitsiConferenceEvents.VIDEO_SIP_GW_AVAILABILITY_CHANGED);
  236. this.chatRoomForwarder.forward(
  237. XMPPEvents.VIDEO_SIP_GW_SESSION_STATE_CHANGED,
  238. JitsiConferenceEvents.VIDEO_SIP_GW_SESSION_STATE_CHANGED);
  239. this.chatRoomForwarder.forward(XMPPEvents.PHONE_NUMBER_CHANGED,
  240. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  241. chatRoom.setParticipantPropertyListener((node, from) => {
  242. const participant = conference.getParticipantById(from);
  243. if (!participant) {
  244. return;
  245. }
  246. participant.setProperty(
  247. node.tagName.substring('jitsi_participant_'.length),
  248. node.value);
  249. });
  250. chatRoom.addListener(XMPPEvents.KICKED,
  251. conference.onMemberKicked.bind(conference));
  252. chatRoom.addListener(XMPPEvents.SUSPEND_DETECTED,
  253. conference.onSuspendDetected.bind(conference));
  254. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOCK_CHANGED,
  255. JitsiConferenceEvents.LOCK_STATE_CHANGED);
  256. this.chatRoomForwarder.forward(XMPPEvents.MUC_MEMBERS_ONLY_CHANGED,
  257. JitsiConferenceEvents.MEMBERS_ONLY_CHANGED);
  258. chatRoom.addListener(XMPPEvents.MUC_MEMBER_JOINED,
  259. conference.onMemberJoined.bind(conference));
  260. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOBBY_MEMBER_JOINED,
  261. JitsiConferenceEvents.LOBBY_USER_JOINED);
  262. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOBBY_MEMBER_UPDATED,
  263. JitsiConferenceEvents.LOBBY_USER_UPDATED);
  264. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOBBY_MEMBER_LEFT,
  265. JitsiConferenceEvents.LOBBY_USER_LEFT);
  266. chatRoom.addListener(XMPPEvents.MUC_MEMBER_BOT_TYPE_CHANGED,
  267. conference._onMemberBotTypeChanged.bind(conference));
  268. chatRoom.addListener(XMPPEvents.MUC_MEMBER_LEFT,
  269. conference.onMemberLeft.bind(conference));
  270. this.chatRoomForwarder.forward(XMPPEvents.MUC_LEFT,
  271. JitsiConferenceEvents.CONFERENCE_LEFT);
  272. this.chatRoomForwarder.forward(XMPPEvents.MUC_DENIED_ACCESS,
  273. JitsiConferenceEvents.CONFERENCE_FAILED,
  274. JitsiConferenceErrors.CONFERENCE_ACCESS_DENIED);
  275. chatRoom.addListener(XMPPEvents.DISPLAY_NAME_CHANGED,
  276. conference.onDisplayNameChanged.bind(conference));
  277. chatRoom.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, role => {
  278. conference.onLocalRoleChanged(role);
  279. });
  280. chatRoom.addListener(XMPPEvents.MUC_ROLE_CHANGED,
  281. conference.onUserRoleChanged.bind(conference));
  282. chatRoom.addListener(AuthenticationEvents.IDENTITY_UPDATED,
  283. (authEnabled, authIdentity) => {
  284. conference.authEnabled = authEnabled;
  285. conference.authIdentity = authIdentity;
  286. conference.eventEmitter.emit(
  287. JitsiConferenceEvents.AUTH_STATUS_CHANGED, authEnabled,
  288. authIdentity);
  289. });
  290. chatRoom.addListener(
  291. XMPPEvents.MESSAGE_RECEIVED,
  292. // eslint-disable-next-line max-params
  293. (jid, txt, myJid, ts, nick, isGuest) => {
  294. const id = Strophe.getResourceFromJid(jid);
  295. conference.eventEmitter.emit(
  296. JitsiConferenceEvents.MESSAGE_RECEIVED,
  297. id, txt, ts, nick, isGuest);
  298. });
  299. chatRoom.addListener(
  300. XMPPEvents.PRIVATE_MESSAGE_RECEIVED,
  301. // eslint-disable-next-line max-params
  302. (jid, txt, myJid, ts) => {
  303. const id = Strophe.getResourceFromJid(jid);
  304. conference.eventEmitter.emit(
  305. JitsiConferenceEvents.PRIVATE_MESSAGE_RECEIVED,
  306. id, txt, ts);
  307. });
  308. chatRoom.addListener(XMPPEvents.PRESENCE_STATUS,
  309. (jid, status) => {
  310. const id = Strophe.getResourceFromJid(jid);
  311. const participant = conference.getParticipantById(id);
  312. if (!participant || participant._status === status) {
  313. return;
  314. }
  315. participant._status = status;
  316. conference.eventEmitter.emit(
  317. JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  318. });
  319. chatRoom.addListener(XMPPEvents.JSON_MESSAGE_RECEIVED,
  320. (from, payload) => {
  321. const id = Strophe.getResourceFromJid(from);
  322. const participant = conference.getParticipantById(id);
  323. if (participant) {
  324. conference.eventEmitter.emit(
  325. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  326. participant, payload);
  327. } else {
  328. conference.eventEmitter.emit(
  329. JitsiConferenceEvents.NON_PARTICIPANT_MESSAGE_RECEIVED,
  330. id, payload);
  331. }
  332. });
  333. chatRoom.addPresenceListener('startmuted', (data, from) => {
  334. // Ignore the strartmuted policy if the presence is received from self. The moderator should join with
  335. // available local sources and the policy needs to be applied only on users that join the call after.
  336. if (conference.myUserId() === from) {
  337. return;
  338. }
  339. const participant = conference.getParticipantById(from);
  340. if (!participant || !participant.isModerator()) {
  341. return;
  342. }
  343. const startAudioMuted = data.attributes.audio === 'true';
  344. const startVideoMuted = data.attributes.video === 'true';
  345. let updated = false;
  346. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  347. conference.startMutedPolicy.audio = startAudioMuted;
  348. updated = true;
  349. }
  350. if (startVideoMuted !== conference.startMutedPolicy.video) {
  351. conference.startMutedPolicy.video = startVideoMuted;
  352. updated = true;
  353. }
  354. if (updated) {
  355. conference.eventEmitter.emit(
  356. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  357. conference.startMutedPolicy
  358. );
  359. }
  360. });
  361. // Breakout rooms.
  362. this.chatRoomForwarder.forward(XMPPEvents.BREAKOUT_ROOMS_MOVE_TO_ROOM,
  363. JitsiConferenceEvents.BREAKOUT_ROOMS_MOVE_TO_ROOM);
  364. this.chatRoomForwarder.forward(XMPPEvents.BREAKOUT_ROOMS_UPDATED,
  365. JitsiConferenceEvents.BREAKOUT_ROOMS_UPDATED);
  366. // Room metadata.
  367. this.chatRoomForwarder.forward(XMPPEvents.ROOM_METADATA_UPDATED,
  368. JitsiConferenceEvents.METADATA_UPDATED);
  369. };
  370. /**
  371. * Setups event listeners related to conference.rtc
  372. */
  373. JitsiConferenceEventManager.prototype.setupRTCListeners = function() {
  374. const conference = this.conference;
  375. const rtc = conference.rtc;
  376. rtc.addListener(
  377. RTCEvents.REMOTE_TRACK_ADDED,
  378. conference.onRemoteTrackAdded.bind(conference));
  379. rtc.addListener(
  380. RTCEvents.REMOTE_TRACK_REMOVED,
  381. conference.onRemoteTrackRemoved.bind(conference));
  382. rtc.addListener(RTCEvents.DOMINANT_SPEAKER_CHANGED,
  383. (dominant, previous, silence) => {
  384. if ((conference.lastDominantSpeaker !== dominant || conference.dominantSpeakerIsSilent !== silence)
  385. && conference.room) {
  386. conference.lastDominantSpeaker = dominant;
  387. conference.dominantSpeakerIsSilent = silence;
  388. conference.eventEmitter.emit(
  389. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, dominant, previous, silence);
  390. if (conference.statistics && conference.myUserId() === dominant) {
  391. // We are the new dominant speaker.
  392. conference.xmpp.sendDominantSpeakerEvent(conference.room.roomjid, silence);
  393. }
  394. if (conference.lastDominantSpeaker !== dominant) {
  395. if (previous && previous.length) {
  396. const speakerList = previous.slice(0);
  397. // Add the dominant speaker to the top of the list (exclude self).
  398. if (conference.myUserId !== dominant) {
  399. speakerList.splice(0, 0, dominant);
  400. }
  401. // Trim the list to the top 5 speakers only.
  402. if (speakerList.length > SPEAKERS_AUDIO_LEVELS) {
  403. speakerList.splice(SPEAKERS_AUDIO_LEVELS, speakerList.length - SPEAKERS_AUDIO_LEVELS);
  404. }
  405. conference.statistics && conference.statistics.setSpeakerList(speakerList);
  406. }
  407. }
  408. }
  409. });
  410. rtc.addListener(RTCEvents.DATA_CHANNEL_OPEN, () => {
  411. const now = window.performance.now();
  412. const key = 'data.channel.opened';
  413. // TODO: Move all of the 'connectionTimes' logic to its own module.
  414. logger.log(`(TIME) ${key}:\t`, now);
  415. conference.room.connectionTimes[key] = now;
  416. Statistics.sendAnalytics(
  417. createConnectionStageReachedEvent(key, { value: now }));
  418. conference.eventEmitter.emit(JitsiConferenceEvents.DATA_CHANNEL_OPENED);
  419. });
  420. rtc.addListener(RTCEvents.DATA_CHANNEL_CLOSED, ev => {
  421. conference.eventEmitter.emit(JitsiConferenceEvents.DATA_CHANNEL_CLOSED, ev);
  422. });
  423. rtc.addListener(RTCEvents.VIDEO_SSRCS_REMAPPED, msg => {
  424. for (const session of this.conference.getMediaSessions()) {
  425. session.processSourceMap(msg, MediaType.VIDEO);
  426. }
  427. });
  428. rtc.addListener(RTCEvents.AUDIO_SSRCS_REMAPPED, msg => {
  429. for (const session of this.conference.getMediaSessions()) {
  430. session.processSourceMap(msg, MediaType.AUDIO);
  431. }
  432. });
  433. rtc.addListener(RTCEvents.ENDPOINT_MESSAGE_RECEIVED,
  434. (from, payload) => {
  435. const participant = conference.getParticipantById(from);
  436. if (participant) {
  437. conference.eventEmitter.emit(
  438. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  439. participant, payload);
  440. } else {
  441. logger.warn(
  442. 'Ignored ENDPOINT_MESSAGE_RECEIVED for not existing '
  443. + `participant: ${from}`,
  444. payload);
  445. }
  446. });
  447. rtc.addListener(RTCEvents.ENDPOINT_STATS_RECEIVED,
  448. (from, payload) => {
  449. const participant = conference.getParticipantById(from);
  450. if (participant) {
  451. conference.eventEmitter.emit(JitsiConferenceEvents.ENDPOINT_STATS_RECEIVED, participant, payload);
  452. } else {
  453. logger.warn(`Ignoring ENDPOINT_STATS_RECEIVED for a non-existant participant: ${from}`);
  454. }
  455. });
  456. rtc.addListener(RTCEvents.CREATE_ANSWER_FAILED,
  457. (e, tpc) => {
  458. if (!tpc.isP2P) {
  459. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  460. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  461. }
  462. });
  463. rtc.addListener(RTCEvents.CREATE_OFFER_FAILED,
  464. (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.SET_LOCAL_DESCRIPTION_FAILED,
  471. (e, tpc) => {
  472. if (!tpc.isP2P) {
  473. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  474. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  475. }
  476. });
  477. rtc.addListener(RTCEvents.SET_REMOTE_DESCRIPTION_FAILED,
  478. (e, tpc) => {
  479. if (!tpc.isP2P) {
  480. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  481. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  482. }
  483. });
  484. };
  485. /**
  486. * Removes event listeners related to conference.xmpp
  487. */
  488. JitsiConferenceEventManager.prototype.removeXMPPListeners = function() {
  489. const conference = this.conference;
  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. this._addConferenceXMPPListener(
  503. XMPPEvents.CALL_INCOMING,
  504. conference.onIncomingCall.bind(conference));
  505. this._addConferenceXMPPListener(
  506. XMPPEvents.CALL_ACCEPTED,
  507. conference.onCallAccepted.bind(conference));
  508. this._addConferenceXMPPListener(
  509. XMPPEvents.TRANSPORT_INFO,
  510. conference.onTransportInfo.bind(conference));
  511. this._addConferenceXMPPListener(
  512. XMPPEvents.CALL_ENDED,
  513. conference.onCallEnded.bind(conference));
  514. this._addConferenceXMPPListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  515. (audioMuted, videoMuted) => {
  516. if (conference.options.config.ignoreStartMuted) {
  517. return;
  518. }
  519. conference.startAudioMuted = audioMuted;
  520. conference.startVideoMuted = videoMuted;
  521. if (audioMuted) {
  522. conference.isMutedByFocus = true;
  523. }
  524. if (videoMuted) {
  525. conference.isVideoMutedByFocus = true;
  526. }
  527. // mute existing local tracks because this is initial mute from
  528. // Jicofo
  529. conference.getLocalTracks().forEach(track => {
  530. switch (track.getType()) {
  531. case MediaType.AUDIO:
  532. conference.startAudioMuted && track.mute();
  533. break;
  534. case MediaType.VIDEO:
  535. conference.startVideoMuted && track.mute();
  536. break;
  537. }
  538. });
  539. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  540. });
  541. this._addConferenceXMPPListener(XMPPEvents.CONFERENCE_TIMESTAMP_RECEIVED,
  542. createdTimestamp => {
  543. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_CREATED_TIMESTAMP, createdTimestamp);
  544. });
  545. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_CHANGED,
  546. (value, mediaType, actorJid) => {
  547. const actorParticipant = conference.getParticipants().find(p => p.getJid() === actorJid);
  548. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_CHANGED, {
  549. enabled: value,
  550. mediaType,
  551. actor: actorParticipant
  552. });
  553. });
  554. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_PARTICIPANT_APPROVED,
  555. (mediaType, jid) => {
  556. const participant = conference.getParticipantById(Strophe.getResourceFromJid(jid));
  557. if (participant) {
  558. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_PARTICIPANT_APPROVED, {
  559. participant,
  560. mediaType
  561. });
  562. }
  563. });
  564. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_PARTICIPANT_REJECTED,
  565. (mediaType, jid) => {
  566. const participant = conference.getParticipantById(Strophe.getResourceFromJid(jid));
  567. if (participant) {
  568. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_PARTICIPANT_REJECTED, {
  569. participant,
  570. mediaType
  571. });
  572. }
  573. });
  574. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_APPROVED,
  575. value => conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_APPROVED, { mediaType: value }));
  576. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_REJECTED,
  577. value => {
  578. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_REJECTED, { mediaType: value });
  579. });
  580. };
  581. /**
  582. * Add XMPP listener and save its reference for remove on leave conference.
  583. */
  584. JitsiConferenceEventManager.prototype._addConferenceXMPPListener = function(
  585. eventName, listener) {
  586. this.xmppListeners[eventName] = listener;
  587. this.conference.xmpp.addListener(eventName, listener);
  588. };
  589. /**
  590. * Setups event listeners related to conference.statistics
  591. */
  592. JitsiConferenceEventManager.prototype.setupStatisticsListeners = function() {
  593. const conference = this.conference;
  594. if (!conference.statistics) {
  595. return;
  596. }
  597. /* eslint-disable max-params */
  598. conference.statistics.addAudioLevelListener((tpc, ssrc, level, isLocal) => {
  599. conference.rtc.setAudioLevel(tpc, ssrc, level, isLocal);
  600. });
  601. /* eslint-enable max-params */
  602. // Forward the "before stats disposed" event
  603. conference.statistics.addBeforeDisposedListener(() => {
  604. conference.eventEmitter.emit(
  605. JitsiConferenceEvents.BEFORE_STATISTICS_DISPOSED);
  606. });
  607. // if we are in startSilent mode we will not be sending/receiving so nothing to detect
  608. if (!conference.options.config.startSilent) {
  609. conference.statistics.addByteSentStatsListener((tpc, stats) => {
  610. conference.getLocalTracks(MediaType.AUDIO).forEach(track => {
  611. const ssrc = tpc.getLocalSSRC(track);
  612. if (!ssrc || !stats.hasOwnProperty(ssrc)) {
  613. return;
  614. }
  615. track.onByteSentStatsReceived(tpc, stats[ssrc]);
  616. });
  617. });
  618. }
  619. };