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 29KB

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