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

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