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

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