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