Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JitsiConferenceEventManager.js 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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((id, prop, value) => {
  227. const participant = conference.getParticipantById(id);
  228. if (!participant) {
  229. return;
  230. }
  231. participant.setProperty(prop, value);
  232. });
  233. chatRoom.addListener(XMPPEvents.KICKED,
  234. conference.onMemberKicked.bind(conference));
  235. chatRoom.addListener(XMPPEvents.SUSPEND_DETECTED,
  236. conference.onSuspendDetected.bind(conference));
  237. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOCK_CHANGED,
  238. JitsiConferenceEvents.LOCK_STATE_CHANGED);
  239. this.chatRoomForwarder.forward(XMPPEvents.MUC_MEMBERS_ONLY_CHANGED,
  240. JitsiConferenceEvents.MEMBERS_ONLY_CHANGED);
  241. this.chatRoomForwarder.forward(XMPPEvents.MUC_VISITORS_SUPPORTED_CHANGED,
  242. JitsiConferenceEvents.VISITORS_SUPPORTED_CHANGED);
  243. chatRoom.addListener(XMPPEvents.MUC_MEMBER_JOINED,
  244. conference.onMemberJoined.bind(conference));
  245. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOBBY_MEMBER_JOINED,
  246. JitsiConferenceEvents.LOBBY_USER_JOINED);
  247. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOBBY_MEMBER_UPDATED,
  248. JitsiConferenceEvents.LOBBY_USER_UPDATED);
  249. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOBBY_MEMBER_LEFT,
  250. JitsiConferenceEvents.LOBBY_USER_LEFT);
  251. chatRoom.addListener(XMPPEvents.MUC_MEMBER_BOT_TYPE_CHANGED,
  252. conference._onMemberBotTypeChanged.bind(conference));
  253. chatRoom.addListener(XMPPEvents.MUC_MEMBER_LEFT,
  254. conference.onMemberLeft.bind(conference));
  255. this.chatRoomForwarder.forward(XMPPEvents.MUC_LEFT,
  256. JitsiConferenceEvents.CONFERENCE_LEFT);
  257. this.chatRoomForwarder.forward(XMPPEvents.MUC_DENIED_ACCESS,
  258. JitsiConferenceEvents.CONFERENCE_FAILED,
  259. JitsiConferenceErrors.CONFERENCE_ACCESS_DENIED);
  260. chatRoom.addListener(XMPPEvents.DISPLAY_NAME_CHANGED,
  261. conference.onDisplayNameChanged.bind(conference));
  262. chatRoom.addListener(XMPPEvents.SILENT_STATUS_CHANGED,
  263. conference.onSilentStatusChanged.bind(conference));
  264. chatRoom.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, role => {
  265. conference.onLocalRoleChanged(role);
  266. });
  267. chatRoom.addListener(XMPPEvents.MUC_ROLE_CHANGED,
  268. conference.onUserRoleChanged.bind(conference));
  269. chatRoom.addListener(AuthenticationEvents.IDENTITY_UPDATED,
  270. (authEnabled, authIdentity) => {
  271. conference.authEnabled = authEnabled;
  272. conference.authIdentity = authIdentity;
  273. conference.eventEmitter.emit(
  274. JitsiConferenceEvents.AUTH_STATUS_CHANGED, authEnabled,
  275. authIdentity);
  276. });
  277. chatRoom.addListener(
  278. XMPPEvents.MESSAGE_RECEIVED,
  279. // eslint-disable-next-line max-params
  280. (jid, txt, myJid, ts, nick, isGuest, messageId) => {
  281. const participantId = Strophe.getResourceFromJid(jid);
  282. conference.eventEmitter.emit(
  283. JitsiConferenceEvents.MESSAGE_RECEIVED,
  284. participantId, txt, ts, nick, isGuest, messageId);
  285. });
  286. chatRoom.addListener(
  287. XMPPEvents.REACTION_RECEIVED,
  288. (jid, reactionList, messageId) => {
  289. const participantId = Strophe.getResourceFromJid(jid);
  290. conference.eventEmitter.emit(
  291. JitsiConferenceEvents.REACTION_RECEIVED,
  292. participantId, reactionList, messageId);
  293. });
  294. chatRoom.addListener(
  295. XMPPEvents.PRIVATE_MESSAGE_RECEIVED,
  296. // eslint-disable-next-line max-params
  297. (jid, txt, myJid, ts, messageId) => {
  298. const participantId = Strophe.getResourceFromJid(jid);
  299. conference.eventEmitter.emit(
  300. JitsiConferenceEvents.PRIVATE_MESSAGE_RECEIVED,
  301. participantId, txt, ts, messageId);
  302. });
  303. chatRoom.addListener(XMPPEvents.PRESENCE_STATUS,
  304. (jid, status) => {
  305. const id = Strophe.getResourceFromJid(jid);
  306. const participant = conference.getParticipantById(id);
  307. if (!participant || participant._status === status) {
  308. return;
  309. }
  310. participant._status = status;
  311. conference.eventEmitter.emit(
  312. JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  313. });
  314. chatRoom.addListener(XMPPEvents.JSON_MESSAGE_RECEIVED,
  315. (from, payload) => {
  316. const id = Strophe.getResourceFromJid(from);
  317. const participant = conference.getParticipantById(id);
  318. if (participant) {
  319. conference.eventEmitter.emit(
  320. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  321. participant, payload);
  322. } else {
  323. conference.eventEmitter.emit(
  324. JitsiConferenceEvents.NON_PARTICIPANT_MESSAGE_RECEIVED,
  325. id, payload);
  326. }
  327. });
  328. chatRoom.addPresenceListener('startmuted', (data, from) => {
  329. // Ignore the strartmuted policy if the presence is received from self. The moderator should join with
  330. // available local sources and the policy needs to be applied only on users that join the call after.
  331. if (conference.myUserId() === from) {
  332. return;
  333. }
  334. const participant = conference.getParticipantById(from);
  335. if (!participant || !participant.isModerator()) {
  336. return;
  337. }
  338. const startAudioMuted = data.attributes.audio === 'true';
  339. const startVideoMuted = data.attributes.video === 'true';
  340. let updated = false;
  341. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  342. conference.startMutedPolicy.audio = startAudioMuted;
  343. updated = true;
  344. }
  345. if (startVideoMuted !== conference.startMutedPolicy.video) {
  346. conference.startMutedPolicy.video = startVideoMuted;
  347. updated = true;
  348. }
  349. if (updated) {
  350. conference.eventEmitter.emit(
  351. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  352. conference.startMutedPolicy
  353. );
  354. }
  355. });
  356. // Breakout rooms.
  357. this.chatRoomForwarder.forward(XMPPEvents.BREAKOUT_ROOMS_MOVE_TO_ROOM,
  358. JitsiConferenceEvents.BREAKOUT_ROOMS_MOVE_TO_ROOM);
  359. this.chatRoomForwarder.forward(XMPPEvents.BREAKOUT_ROOMS_UPDATED,
  360. JitsiConferenceEvents.BREAKOUT_ROOMS_UPDATED);
  361. // Room metadata.
  362. this.chatRoomForwarder.forward(XMPPEvents.ROOM_METADATA_UPDATED,
  363. JitsiConferenceEvents.METADATA_UPDATED);
  364. };
  365. /**
  366. * Setups event listeners related to conference.rtc
  367. */
  368. JitsiConferenceEventManager.prototype.setupRTCListeners = function() {
  369. const conference = this.conference;
  370. const rtc = conference.rtc;
  371. rtc.addListener(
  372. RTCEvents.REMOTE_TRACK_ADDED,
  373. conference.onRemoteTrackAdded.bind(conference));
  374. rtc.addListener(
  375. RTCEvents.REMOTE_TRACK_REMOVED,
  376. conference.onRemoteTrackRemoved.bind(conference));
  377. rtc.addListener(RTCEvents.DOMINANT_SPEAKER_CHANGED,
  378. (dominant, previous, silence) => {
  379. if ((conference.lastDominantSpeaker !== dominant || conference.dominantSpeakerIsSilent !== silence)
  380. && conference.room) {
  381. conference.lastDominantSpeaker = dominant;
  382. conference.dominantSpeakerIsSilent = silence;
  383. conference.eventEmitter.emit(
  384. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, dominant, previous, silence);
  385. if (conference.statistics && conference.myUserId() === dominant) {
  386. // We are the new dominant speaker.
  387. conference.xmpp.sendDominantSpeakerEvent(conference.room.roomjid, silence);
  388. }
  389. if (conference.lastDominantSpeaker !== dominant) {
  390. if (previous && previous.length) {
  391. const speakerList = previous.slice(0);
  392. // Add the dominant speaker to the top of the list (exclude self).
  393. if (conference.myUserId !== dominant) {
  394. speakerList.splice(0, 0, dominant);
  395. }
  396. // Trim the list to the top 5 speakers only.
  397. if (speakerList.length > SPEAKERS_AUDIO_LEVELS) {
  398. speakerList.splice(SPEAKERS_AUDIO_LEVELS, speakerList.length - SPEAKERS_AUDIO_LEVELS);
  399. }
  400. conference.statistics && conference.statistics.setSpeakerList(speakerList);
  401. }
  402. }
  403. }
  404. });
  405. rtc.addListener(RTCEvents.DATA_CHANNEL_OPEN, () => {
  406. const now = window.performance.now();
  407. const key = 'data.channel.opened';
  408. // TODO: Move all of the 'connectionTimes' logic to its own module.
  409. logger.log(`(TIME) ${key}:\t`, now);
  410. conference.room.connectionTimes[key] = now;
  411. Statistics.sendAnalytics(
  412. createConnectionStageReachedEvent(key, { value: now }));
  413. conference.eventEmitter.emit(JitsiConferenceEvents.DATA_CHANNEL_OPENED);
  414. });
  415. rtc.addListener(RTCEvents.DATA_CHANNEL_CLOSED, ev => {
  416. conference.eventEmitter.emit(JitsiConferenceEvents.DATA_CHANNEL_CLOSED, ev);
  417. });
  418. rtc.addListener(RTCEvents.VIDEO_SSRCS_REMAPPED, msg => {
  419. this.conference.jvbJingleSession.processSourceMap(msg, MediaType.VIDEO);
  420. });
  421. rtc.addListener(RTCEvents.AUDIO_SSRCS_REMAPPED, msg => {
  422. this.conference.jvbJingleSession.processSourceMap(msg, MediaType.AUDIO);
  423. });
  424. rtc.addListener(RTCEvents.ENDPOINT_MESSAGE_RECEIVED,
  425. (from, payload) => {
  426. const participant = conference.getParticipantById(from);
  427. if (participant) {
  428. conference.eventEmitter.emit(
  429. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  430. participant, payload);
  431. } else {
  432. logger.warn(
  433. 'Ignored ENDPOINT_MESSAGE_RECEIVED for not existing '
  434. + `participant: ${from}`,
  435. payload);
  436. }
  437. });
  438. rtc.addListener(RTCEvents.ENDPOINT_STATS_RECEIVED,
  439. (from, payload) => {
  440. const participant = conference.getParticipantById(from);
  441. if (participant) {
  442. conference.eventEmitter.emit(JitsiConferenceEvents.ENDPOINT_STATS_RECEIVED, participant, payload);
  443. } else {
  444. logger.warn(`Ignoring ENDPOINT_STATS_RECEIVED for a non-existant participant: ${from}`);
  445. }
  446. });
  447. rtc.addListener(RTCEvents.CREATE_ANSWER_FAILED,
  448. (e, tpc) => {
  449. if (!tpc.isP2P) {
  450. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  451. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  452. }
  453. });
  454. rtc.addListener(RTCEvents.CREATE_OFFER_FAILED,
  455. (e, tpc) => {
  456. if (!tpc.isP2P) {
  457. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  458. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  459. }
  460. });
  461. rtc.addListener(RTCEvents.SET_LOCAL_DESCRIPTION_FAILED,
  462. (e, tpc) => {
  463. if (!tpc.isP2P) {
  464. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  465. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  466. }
  467. });
  468. rtc.addListener(RTCEvents.SET_REMOTE_DESCRIPTION_FAILED,
  469. (e, tpc) => {
  470. if (!tpc.isP2P) {
  471. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  472. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  473. }
  474. });
  475. };
  476. /**
  477. * Removes event listeners related to conference.xmpp
  478. */
  479. JitsiConferenceEventManager.prototype.removeXMPPListeners = function() {
  480. const conference = this.conference;
  481. Object.keys(this.xmppListeners).forEach(eventName => {
  482. conference.xmpp.removeListener(
  483. eventName,
  484. this.xmppListeners[eventName]);
  485. });
  486. this.xmppListeners = {};
  487. };
  488. /**
  489. * Setups event listeners related to conference.xmpp
  490. */
  491. JitsiConferenceEventManager.prototype.setupXMPPListeners = function() {
  492. const conference = this.conference;
  493. this._addConferenceXMPPListener(
  494. XMPPEvents.CALL_INCOMING,
  495. conference.onIncomingCall.bind(conference));
  496. this._addConferenceXMPPListener(
  497. XMPPEvents.CALL_ACCEPTED,
  498. conference.onCallAccepted.bind(conference));
  499. this._addConferenceXMPPListener(
  500. XMPPEvents.TRANSPORT_INFO,
  501. conference.onTransportInfo.bind(conference));
  502. this._addConferenceXMPPListener(
  503. XMPPEvents.CALL_ENDED,
  504. conference.onCallEnded.bind(conference));
  505. this._addConferenceXMPPListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  506. (audioMuted, videoMuted) => {
  507. if (conference.options.config.ignoreStartMuted) {
  508. return;
  509. }
  510. conference.startAudioMuted = audioMuted;
  511. conference.startVideoMuted = videoMuted;
  512. if (audioMuted) {
  513. conference.isMutedByFocus = true;
  514. }
  515. if (videoMuted) {
  516. conference.isVideoMutedByFocus = true;
  517. }
  518. // mute existing local tracks because this is initial mute from
  519. // Jicofo
  520. conference.getLocalTracks().forEach(track => {
  521. switch (track.getType()) {
  522. case MediaType.AUDIO:
  523. conference.startAudioMuted && track.mute();
  524. break;
  525. case MediaType.VIDEO:
  526. conference.startVideoMuted && track.mute();
  527. break;
  528. }
  529. });
  530. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  531. });
  532. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_CHANGED,
  533. (value, mediaType, actorJid) => {
  534. const actorParticipant = conference.getParticipants().find(p => p.getJid() === actorJid);
  535. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_CHANGED, {
  536. enabled: value,
  537. mediaType,
  538. actor: actorParticipant
  539. });
  540. });
  541. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_PARTICIPANT_APPROVED,
  542. (mediaType, jid) => {
  543. const participant = conference.getParticipantById(Strophe.getResourceFromJid(jid));
  544. if (participant) {
  545. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_PARTICIPANT_APPROVED, {
  546. participant,
  547. mediaType
  548. });
  549. }
  550. });
  551. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_PARTICIPANT_REJECTED,
  552. (mediaType, jid) => {
  553. const participant = conference.getParticipantById(Strophe.getResourceFromJid(jid));
  554. if (participant) {
  555. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_PARTICIPANT_REJECTED, {
  556. participant,
  557. mediaType
  558. });
  559. }
  560. });
  561. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_APPROVED,
  562. value => conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_APPROVED, { mediaType: value }));
  563. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_REJECTED,
  564. value => {
  565. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_REJECTED, { mediaType: value });
  566. });
  567. this._addConferenceXMPPListener(XMPPEvents.VISITORS_MESSAGE,
  568. value => conference.eventEmitter.emit(JitsiConferenceEvents.VISITORS_MESSAGE, value));
  569. this._addConferenceXMPPListener(XMPPEvents.VISITORS_REJECTION,
  570. () => conference.eventEmitter.emit(JitsiConferenceEvents.VISITORS_REJECTION));
  571. };
  572. /**
  573. * Add XMPP listener and save its reference for remove on leave conference.
  574. */
  575. JitsiConferenceEventManager.prototype._addConferenceXMPPListener = function(
  576. eventName, listener) {
  577. this.xmppListeners[eventName] = listener;
  578. this.conference.xmpp.addListener(eventName, listener);
  579. };
  580. /**
  581. * Setups event listeners related to conference.statistics
  582. */
  583. JitsiConferenceEventManager.prototype.setupStatisticsListeners = function() {
  584. const conference = this.conference;
  585. if (!conference.statistics) {
  586. return;
  587. }
  588. /* eslint-disable max-params */
  589. conference.statistics.addAudioLevelListener((tpc, ssrc, level, isLocal) => {
  590. conference.rtc.setAudioLevel(tpc, ssrc, level, isLocal);
  591. });
  592. /* eslint-enable max-params */
  593. // Forward the "before stats disposed" event
  594. conference.statistics.addBeforeDisposedListener(() => {
  595. conference.eventEmitter.emit(
  596. JitsiConferenceEvents.BEFORE_STATISTICS_DISPOSED);
  597. });
  598. conference.statistics.addEncodeTimeStatsListener((tpc, stats) => {
  599. conference.eventEmitter.emit(
  600. JitsiConferenceEvents.ENCODE_TIME_STATS_RECEIVED, tpc, stats);
  601. });
  602. // if we are in startSilent mode we will not be sending/receiving so nothing to detect
  603. if (!conference.options.config.startSilent) {
  604. conference.statistics.addByteSentStatsListener((tpc, stats) => {
  605. conference.getLocalTracks(MediaType.AUDIO).forEach(track => {
  606. const ssrc = tpc.getLocalSSRC(track);
  607. if (!ssrc || !stats.hasOwnProperty(ssrc)) {
  608. return;
  609. }
  610. track.onByteSentStatsReceived(tpc, stats[ssrc]);
  611. });
  612. });
  613. }
  614. };