Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

JitsiConferenceEventManager.js 28KB

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