modified lib-jitsi-meet dev repo
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 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 { VideoType } from './service/RTC/VideoType';
  12. import AuthenticationEvents
  13. from './service/authentication/AuthenticationEvents';
  14. import {
  15. ACTION_JINGLE_SA_TIMEOUT,
  16. createBridgeDownEvent,
  17. createConnectionStageReachedEvent,
  18. createFocusLeftEvent,
  19. createJingleEvent,
  20. createRemotelyMutedEvent
  21. } from './service/statistics/AnalyticsEvents';
  22. import { XMPPEvents } from './service/xmpp/XMPPEvents';
  23. const logger = getLogger(__filename);
  24. /**
  25. * Setups all event listeners related to conference
  26. * @param conference {JitsiConference} the conference
  27. */
  28. export default function JitsiConferenceEventManager(conference) {
  29. this.conference = conference;
  30. this.xmppListeners = {};
  31. // Listeners related to the conference only
  32. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  33. track => {
  34. if (!track.isLocal() || !conference.statistics) {
  35. return;
  36. }
  37. const session
  38. = track.isP2P
  39. ? conference.p2pJingleSession : conference.jvbJingleSession;
  40. // TPC will be null, before the conference starts, but the event
  41. // still should be queued
  42. const tpc = (session && session.peerconnection) || null;
  43. conference.statistics.sendMuteEvent(
  44. tpc,
  45. track.isMuted(),
  46. track.getType());
  47. });
  48. }
  49. /**
  50. * Setups event listeners related to conference.chatRoom
  51. */
  52. JitsiConferenceEventManager.prototype.setupChatRoomListeners = function() {
  53. const conference = this.conference;
  54. const chatRoom = conference.room;
  55. this.chatRoomForwarder = new EventEmitterForwarder(chatRoom,
  56. this.conference.eventEmitter);
  57. chatRoom.addListener(XMPPEvents.ICE_RESTARTING, jingleSession => {
  58. if (!jingleSession.isP2P) {
  59. // If using DataChannel as bridge channel, it must be closed
  60. // before ICE restart, otherwise Chrome will not trigger "opened"
  61. // event for the channel established with the new bridge.
  62. // TODO: This may be bypassed when using a WebSocket as bridge
  63. // channel.
  64. conference.rtc.closeBridgeChannel();
  65. }
  66. // else: there are no DataChannels in P2P session (at least for now)
  67. });
  68. chatRoom.addListener(XMPPEvents.PARTICIPANT_FEATURES_CHANGED, (from, features) => {
  69. const participant = conference.getParticipantById(Strophe.getResourceFromJid(from));
  70. if (participant) {
  71. participant.setFeatures(features);
  72. conference.eventEmitter.emit(JitsiConferenceEvents.PARTCIPANT_FEATURES_CHANGED, participant);
  73. }
  74. });
  75. chatRoom.addListener(
  76. XMPPEvents.ICE_RESTART_SUCCESS,
  77. (jingleSession, offerIq) => {
  78. // The JVB data chanel needs to be reopened in case the conference
  79. // has been moved to a new bridge.
  80. !jingleSession.isP2P
  81. && conference._setBridgeChannel(
  82. offerIq, jingleSession.peerconnection);
  83. });
  84. chatRoom.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  85. actor => {
  86. // TODO: Add a way to differentiate between commands which caused
  87. // us to mute and those that did not change our state (i.e. we were
  88. // already muted).
  89. Statistics.sendAnalytics(createRemotelyMutedEvent(MediaType.AUDIO));
  90. conference.mutedByFocusActor = actor;
  91. // set isMutedByFocus when setAudioMute Promise ends
  92. conference.rtc.setAudioMute(true).then(
  93. () => {
  94. conference.isMutedByFocus = true;
  95. conference.mutedByFocusActor = null;
  96. })
  97. .catch(
  98. error => {
  99. conference.mutedByFocusActor = null;
  100. logger.warn(
  101. 'Error while audio muting due to focus request', error);
  102. });
  103. }
  104. );
  105. chatRoom.addListener(XMPPEvents.VIDEO_MUTED_BY_FOCUS,
  106. actor => {
  107. // TODO: Add a way to differentiate between commands which caused
  108. // us to mute and those that did not change our state (i.e. we were
  109. // already muted).
  110. Statistics.sendAnalytics(createRemotelyMutedEvent(MediaType.VIDEO));
  111. conference.mutedVideoByFocusActor = actor;
  112. // set isVideoMutedByFocus when setVideoMute Promise ends
  113. conference.rtc.setVideoMute(true).then(
  114. () => {
  115. conference.isVideoMutedByFocus = true;
  116. conference.mutedVideoByFocusActor = null;
  117. })
  118. .catch(
  119. error => {
  120. conference.mutedVideoByFocusActor = null;
  121. logger.warn(
  122. 'Error while video muting due to focus request', error);
  123. });
  124. }
  125. );
  126. this.chatRoomForwarder.forward(XMPPEvents.SUBJECT_CHANGED,
  127. JitsiConferenceEvents.SUBJECT_CHANGED);
  128. this.chatRoomForwarder.forward(XMPPEvents.MUC_JOINED,
  129. JitsiConferenceEvents.CONFERENCE_JOINED);
  130. this.chatRoomForwarder.forward(XMPPEvents.MUC_JOIN_IN_PROGRESS,
  131. JitsiConferenceEvents.CONFERENCE_JOIN_IN_PROGRESS);
  132. this.chatRoomForwarder.forward(XMPPEvents.MEETING_ID_SET,
  133. JitsiConferenceEvents.CONFERENCE_UNIQUE_ID_SET);
  134. // send some analytics events
  135. chatRoom.addListener(XMPPEvents.MUC_JOINED,
  136. () => {
  137. this.conference._onMucJoined();
  138. this.conference.isJvbConnectionInterrupted = false;
  139. // TODO: Move all of the 'connectionTimes' logic to its own module.
  140. Object.keys(chatRoom.connectionTimes).forEach(key => {
  141. const event
  142. = createConnectionStageReachedEvent(
  143. `conference_${key}`,
  144. { value: chatRoom.connectionTimes[key] });
  145. Statistics.sendAnalytics(event);
  146. });
  147. // TODO: Move all of the 'connectionTimes' logic to its own module.
  148. Object.keys(chatRoom.xmpp.connectionTimes).forEach(key => {
  149. const event
  150. = createConnectionStageReachedEvent(
  151. `xmpp_${key}`,
  152. { value: chatRoom.xmpp.connectionTimes[key] });
  153. Statistics.sendAnalytics(event);
  154. });
  155. });
  156. chatRoom.addListener(XMPPEvents.RENEGOTIATION_FAILED, (e, session) => {
  157. if (!session.isP2P) {
  158. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  159. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  160. }
  161. });
  162. chatRoom.addListener(JitsiTrackEvents.TRACK_REMOVED, track => {
  163. conference.eventEmitter.emit(JitsiConferenceEvents.TRACK_REMOVED, track);
  164. });
  165. this.chatRoomForwarder.forward(XMPPEvents.ROOM_JOIN_ERROR,
  166. JitsiConferenceEvents.CONFERENCE_FAILED,
  167. JitsiConferenceErrors.CONNECTION_ERROR);
  168. this.chatRoomForwarder.forward(XMPPEvents.ROOM_CONNECT_ERROR,
  169. JitsiConferenceEvents.CONFERENCE_FAILED,
  170. JitsiConferenceErrors.CONNECTION_ERROR);
  171. this.chatRoomForwarder.forward(XMPPEvents.ROOM_CONNECT_NOT_ALLOWED_ERROR,
  172. JitsiConferenceEvents.CONFERENCE_FAILED,
  173. JitsiConferenceErrors.NOT_ALLOWED_ERROR);
  174. this.chatRoomForwarder.forward(XMPPEvents.ROOM_CONNECT_MEMBERS_ONLY_ERROR,
  175. JitsiConferenceEvents.CONFERENCE_FAILED,
  176. JitsiConferenceErrors.MEMBERS_ONLY_ERROR);
  177. this.chatRoomForwarder.forward(XMPPEvents.ROOM_MAX_USERS_ERROR,
  178. JitsiConferenceEvents.CONFERENCE_FAILED,
  179. JitsiConferenceErrors.CONFERENCE_MAX_USERS);
  180. chatRoom.addListener(XMPPEvents.ROOM_MAX_USERS_ERROR, () => conference.leave());
  181. this.chatRoomForwarder.forward(XMPPEvents.PASSWORD_REQUIRED,
  182. JitsiConferenceEvents.CONFERENCE_FAILED,
  183. JitsiConferenceErrors.PASSWORD_REQUIRED);
  184. this.chatRoomForwarder.forward(XMPPEvents.AUTHENTICATION_REQUIRED,
  185. JitsiConferenceEvents.CONFERENCE_FAILED,
  186. JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  187. this.chatRoomForwarder.forward(XMPPEvents.REDIRECTED,
  188. JitsiConferenceEvents.CONFERENCE_FAILED,
  189. JitsiConferenceErrors.REDIRECTED);
  190. this.chatRoomForwarder.forward(XMPPEvents.BRIDGE_DOWN,
  191. JitsiConferenceEvents.CONFERENCE_FAILED,
  192. JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  193. chatRoom.addListener(
  194. XMPPEvents.BRIDGE_DOWN,
  195. () => Statistics.sendAnalytics(createBridgeDownEvent()));
  196. chatRoom.addListener(XMPPEvents.CONNECTION_RESTARTED,
  197. jingleSession => {
  198. conference._onConferenceRestarted(jingleSession);
  199. });
  200. this.chatRoomForwarder.forward(XMPPEvents.RESERVATION_ERROR,
  201. JitsiConferenceEvents.CONFERENCE_FAILED,
  202. JitsiConferenceErrors.RESERVATION_ERROR);
  203. chatRoom.addListener(XMPPEvents.RESERVATION_ERROR, () => conference.leave());
  204. this.chatRoomForwarder.forward(XMPPEvents.GRACEFUL_SHUTDOWN,
  205. JitsiConferenceEvents.CONFERENCE_FAILED,
  206. JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  207. chatRoom.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
  208. jingleSession => {
  209. conference._onIceConnectionFailed(jingleSession);
  210. });
  211. this.chatRoomForwarder.forward(XMPPEvents.MUC_DESTROYED,
  212. JitsiConferenceEvents.CONFERENCE_FAILED,
  213. JitsiConferenceErrors.CONFERENCE_DESTROYED);
  214. chatRoom.addListener(XMPPEvents.MUC_DESTROYED, () => conference.leave());
  215. this.chatRoomForwarder.forward(XMPPEvents.CHAT_ERROR_RECEIVED,
  216. JitsiConferenceEvents.CONFERENCE_ERROR,
  217. JitsiConferenceErrors.CHAT_ERROR);
  218. this.chatRoomForwarder.forward(XMPPEvents.SETTINGS_ERROR_RECEIVED,
  219. JitsiConferenceEvents.CONFERENCE_ERROR,
  220. JitsiConferenceErrors.SETTINGS_ERROR);
  221. this.chatRoomForwarder.forward(XMPPEvents.FOCUS_DISCONNECTED,
  222. JitsiConferenceEvents.CONFERENCE_FAILED,
  223. JitsiConferenceErrors.FOCUS_DISCONNECTED);
  224. chatRoom.addListener(XMPPEvents.FOCUS_LEFT,
  225. () => {
  226. Statistics.sendAnalytics(createFocusLeftEvent());
  227. conference.eventEmitter.emit(
  228. JitsiConferenceEvents.CONFERENCE_FAILED,
  229. JitsiConferenceErrors.FOCUS_LEFT);
  230. });
  231. chatRoom.addListener(XMPPEvents.SESSION_ACCEPT_TIMEOUT,
  232. jingleSession => {
  233. Statistics.sendAnalyticsAndLog(
  234. createJingleEvent(
  235. ACTION_JINGLE_SA_TIMEOUT,
  236. { p2p: jingleSession.isP2P }));
  237. });
  238. chatRoom.addListener(XMPPEvents.RECORDER_STATE_CHANGED,
  239. (session, jid) => {
  240. if (jid) {
  241. const resource = Strophe.getResourceFromJid(jid);
  242. const participant = conference.getParticipantById(resource) || resource;
  243. if (session.getStatus() === 'off') {
  244. session.setTerminator(participant);
  245. } else if (session.getStatus() === 'on') {
  246. session.setInitiator(participant);
  247. }
  248. }
  249. conference.eventEmitter.emit(
  250. JitsiConferenceEvents.RECORDER_STATE_CHANGED,
  251. session);
  252. });
  253. this.chatRoomForwarder.forward(XMPPEvents.TRANSCRIPTION_STATUS_CHANGED,
  254. JitsiConferenceEvents.TRANSCRIPTION_STATUS_CHANGED);
  255. this.chatRoomForwarder.forward(XMPPEvents.VIDEO_SIP_GW_AVAILABILITY_CHANGED,
  256. JitsiConferenceEvents.VIDEO_SIP_GW_AVAILABILITY_CHANGED);
  257. this.chatRoomForwarder.forward(
  258. XMPPEvents.VIDEO_SIP_GW_SESSION_STATE_CHANGED,
  259. JitsiConferenceEvents.VIDEO_SIP_GW_SESSION_STATE_CHANGED);
  260. this.chatRoomForwarder.forward(XMPPEvents.PHONE_NUMBER_CHANGED,
  261. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  262. chatRoom.setParticipantPropertyListener((node, from) => {
  263. const participant = conference.getParticipantById(from);
  264. if (!participant) {
  265. return;
  266. }
  267. participant.setProperty(
  268. node.tagName.substring('jitsi_participant_'.length),
  269. node.value);
  270. });
  271. chatRoom.addListener(XMPPEvents.KICKED,
  272. conference.onMemberKicked.bind(conference));
  273. chatRoom.addListener(XMPPEvents.SUSPEND_DETECTED,
  274. conference.onSuspendDetected.bind(conference));
  275. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOCK_CHANGED,
  276. JitsiConferenceEvents.LOCK_STATE_CHANGED);
  277. this.chatRoomForwarder.forward(XMPPEvents.MUC_MEMBERS_ONLY_CHANGED,
  278. JitsiConferenceEvents.MEMBERS_ONLY_CHANGED);
  279. chatRoom.addListener(XMPPEvents.MUC_MEMBER_JOINED,
  280. conference.onMemberJoined.bind(conference));
  281. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOBBY_MEMBER_JOINED,
  282. JitsiConferenceEvents.LOBBY_USER_JOINED);
  283. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOBBY_MEMBER_UPDATED,
  284. JitsiConferenceEvents.LOBBY_USER_UPDATED);
  285. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOBBY_MEMBER_LEFT,
  286. JitsiConferenceEvents.LOBBY_USER_LEFT);
  287. chatRoom.addListener(XMPPEvents.MUC_MEMBER_BOT_TYPE_CHANGED,
  288. conference._onMemberBotTypeChanged.bind(conference));
  289. chatRoom.addListener(XMPPEvents.MUC_MEMBER_LEFT,
  290. conference.onMemberLeft.bind(conference));
  291. this.chatRoomForwarder.forward(XMPPEvents.MUC_LEFT,
  292. JitsiConferenceEvents.CONFERENCE_LEFT);
  293. this.chatRoomForwarder.forward(XMPPEvents.MUC_DENIED_ACCESS,
  294. JitsiConferenceEvents.CONFERENCE_FAILED,
  295. JitsiConferenceErrors.CONFERENCE_ACCESS_DENIED);
  296. chatRoom.addListener(XMPPEvents.DISPLAY_NAME_CHANGED,
  297. conference.onDisplayNameChanged.bind(conference));
  298. chatRoom.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, role => {
  299. conference.onLocalRoleChanged(role);
  300. // log all events for the recorder operated by the moderator
  301. if (conference.statistics && conference.isModerator()) {
  302. conference.on(JitsiConferenceEvents.RECORDER_STATE_CHANGED,
  303. recorderSession => {
  304. const logObject = {
  305. error: recorderSession.getError(),
  306. id: 'recorder_status',
  307. status: recorderSession.getStatus()
  308. };
  309. Statistics.sendLog(JSON.stringify(logObject));
  310. });
  311. }
  312. });
  313. chatRoom.addListener(XMPPEvents.MUC_ROLE_CHANGED,
  314. conference.onUserRoleChanged.bind(conference));
  315. chatRoom.addListener(AuthenticationEvents.IDENTITY_UPDATED,
  316. (authEnabled, authIdentity) => {
  317. conference.authEnabled = authEnabled;
  318. conference.authIdentity = authIdentity;
  319. conference.eventEmitter.emit(
  320. JitsiConferenceEvents.AUTH_STATUS_CHANGED, authEnabled,
  321. authIdentity);
  322. });
  323. chatRoom.addListener(
  324. XMPPEvents.MESSAGE_RECEIVED,
  325. // eslint-disable-next-line max-params
  326. (jid, txt, myJid, ts) => {
  327. const id = Strophe.getResourceFromJid(jid);
  328. conference.eventEmitter.emit(
  329. JitsiConferenceEvents.MESSAGE_RECEIVED,
  330. id, txt, ts);
  331. });
  332. chatRoom.addListener(
  333. XMPPEvents.PRIVATE_MESSAGE_RECEIVED,
  334. // eslint-disable-next-line max-params
  335. (jid, txt, myJid, ts) => {
  336. const id = Strophe.getResourceFromJid(jid);
  337. conference.eventEmitter.emit(
  338. JitsiConferenceEvents.PRIVATE_MESSAGE_RECEIVED,
  339. id, txt, ts);
  340. });
  341. chatRoom.addListener(XMPPEvents.PRESENCE_STATUS,
  342. (jid, status) => {
  343. const id = Strophe.getResourceFromJid(jid);
  344. const participant = conference.getParticipantById(id);
  345. if (!participant || participant._status === status) {
  346. return;
  347. }
  348. participant._status = status;
  349. conference.eventEmitter.emit(
  350. JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  351. });
  352. chatRoom.addListener(XMPPEvents.JSON_MESSAGE_RECEIVED,
  353. (from, payload) => {
  354. const id = Strophe.getResourceFromJid(from);
  355. const participant = conference.getParticipantById(id);
  356. if (participant) {
  357. conference.eventEmitter.emit(
  358. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  359. participant, payload);
  360. } else {
  361. conference.eventEmitter.emit(
  362. JitsiConferenceEvents.NON_PARTICIPANT_MESSAGE_RECEIVED,
  363. id, payload);
  364. }
  365. });
  366. chatRoom.addPresenceListener('startmuted', (data, from) => {
  367. let isModerator = false;
  368. if (conference.myUserId() === from && conference.isModerator()) {
  369. isModerator = true;
  370. } else {
  371. const participant = conference.getParticipantById(from);
  372. if (participant && participant.isModerator()) {
  373. isModerator = true;
  374. }
  375. }
  376. if (!isModerator) {
  377. return;
  378. }
  379. const startAudioMuted = data.attributes.audio === 'true';
  380. const startVideoMuted = data.attributes.video === 'true';
  381. let updated = false;
  382. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  383. conference.startMutedPolicy.audio = startAudioMuted;
  384. updated = true;
  385. }
  386. if (startVideoMuted !== conference.startMutedPolicy.video) {
  387. conference.startMutedPolicy.video = startVideoMuted;
  388. updated = true;
  389. }
  390. if (updated) {
  391. conference.eventEmitter.emit(
  392. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  393. conference.startMutedPolicy
  394. );
  395. }
  396. });
  397. if (conference.statistics) {
  398. // FIXME ICE related events should end up in RTCEvents eventually
  399. chatRoom.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
  400. session => {
  401. conference.statistics.sendIceConnectionFailedEvent(
  402. session.peerconnection);
  403. });
  404. // FIXME XMPPEvents.ADD_ICE_CANDIDATE_FAILED is never emitted
  405. chatRoom.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  406. (e, pc) => {
  407. conference.statistics.sendAddIceCandidateFailed(e, pc);
  408. });
  409. }
  410. // Breakout rooms.
  411. this.chatRoomForwarder.forward(XMPPEvents.BREAKOUT_ROOMS_MOVE_TO_ROOM,
  412. JitsiConferenceEvents.BREAKOUT_ROOMS_MOVE_TO_ROOM);
  413. this.chatRoomForwarder.forward(XMPPEvents.BREAKOUT_ROOMS_UPDATED,
  414. JitsiConferenceEvents.BREAKOUT_ROOMS_UPDATED);
  415. // Room metadata.
  416. this.chatRoomForwarder.forward(XMPPEvents.ROOM_METADATA_UPDATED,
  417. JitsiConferenceEvents.METADATA_UPDATED);
  418. };
  419. /**
  420. * Setups event listeners related to conference.rtc
  421. */
  422. JitsiConferenceEventManager.prototype.setupRTCListeners = function() {
  423. const conference = this.conference;
  424. const rtc = conference.rtc;
  425. rtc.addListener(
  426. RTCEvents.REMOTE_TRACK_ADDED,
  427. conference.onRemoteTrackAdded.bind(conference));
  428. rtc.addListener(
  429. RTCEvents.REMOTE_TRACK_REMOVED,
  430. conference.onRemoteTrackRemoved.bind(conference));
  431. rtc.addListener(RTCEvents.DOMINANT_SPEAKER_CHANGED,
  432. (dominant, previous, silence) => {
  433. if ((conference.lastDominantSpeaker !== dominant || conference.dominantSpeakerIsSilent !== silence)
  434. && conference.room) {
  435. conference.lastDominantSpeaker = dominant;
  436. conference.dominantSpeakerIsSilent = silence;
  437. conference.eventEmitter.emit(
  438. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, dominant, previous, silence);
  439. if (conference.statistics && conference.myUserId() === dominant) {
  440. // We are the new dominant speaker.
  441. conference.statistics.sendDominantSpeakerEvent(conference.room.roomjid, silence);
  442. }
  443. if (conference.lastDominantSpeaker !== dominant) {
  444. if (previous && previous.length) {
  445. const speakerList = previous.slice(0);
  446. // Add the dominant speaker to the top of the list (exclude self).
  447. if (conference.myUserId !== dominant) {
  448. speakerList.splice(0, 0, dominant);
  449. }
  450. // Trim the list to the top 5 speakers only.
  451. if (speakerList.length > SPEAKERS_AUDIO_LEVELS) {
  452. speakerList.splice(SPEAKERS_AUDIO_LEVELS, speakerList.length - SPEAKERS_AUDIO_LEVELS);
  453. }
  454. conference.statistics && conference.statistics.setSpeakerList(speakerList);
  455. }
  456. }
  457. }
  458. });
  459. rtc.addListener(RTCEvents.DATA_CHANNEL_OPEN, () => {
  460. const now = window.performance.now();
  461. const key = 'data.channel.opened';
  462. // TODO: Move all of the 'connectionTimes' logic to its own module.
  463. logger.log(`(TIME) ${key}:\t`, now);
  464. conference.room.connectionTimes[key] = now;
  465. Statistics.sendAnalytics(
  466. createConnectionStageReachedEvent(key, { value: now }));
  467. conference.eventEmitter.emit(JitsiConferenceEvents.DATA_CHANNEL_OPENED);
  468. });
  469. rtc.addListener(RTCEvents.DATA_CHANNEL_CLOSED, ev => {
  470. conference.eventEmitter.emit(JitsiConferenceEvents.DATA_CHANNEL_CLOSED, ev);
  471. });
  472. rtc.addListener(RTCEvents.VIDEO_SSRCS_REMAPPED, msg => {
  473. for (const session of this.conference.getMediaSessions()) {
  474. session.processSourceMap(msg, MediaType.VIDEO);
  475. }
  476. });
  477. rtc.addListener(RTCEvents.AUDIO_SSRCS_REMAPPED, msg => {
  478. for (const session of this.conference.getMediaSessions()) {
  479. session.processSourceMap(msg, MediaType.AUDIO);
  480. }
  481. });
  482. rtc.addListener(RTCEvents.ENDPOINT_MESSAGE_RECEIVED,
  483. (from, payload) => {
  484. const participant = conference.getParticipantById(from);
  485. if (participant) {
  486. conference.eventEmitter.emit(
  487. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  488. participant, payload);
  489. } else {
  490. logger.warn(
  491. 'Ignored ENDPOINT_MESSAGE_RECEIVED for not existing '
  492. + `participant: ${from}`,
  493. payload);
  494. }
  495. });
  496. rtc.addListener(RTCEvents.ENDPOINT_STATS_RECEIVED,
  497. (from, payload) => {
  498. const participant = conference.getParticipantById(from);
  499. if (participant) {
  500. conference.eventEmitter.emit(JitsiConferenceEvents.ENDPOINT_STATS_RECEIVED, participant, payload);
  501. } else {
  502. logger.warn(`Ignoring ENDPOINT_STATS_RECEIVED for a non-existant participant: ${from}`);
  503. }
  504. });
  505. rtc.addListener(RTCEvents.LOCAL_UFRAG_CHANGED,
  506. (tpc, ufrag) => {
  507. if (!tpc.isP2P) {
  508. Statistics.sendLog(
  509. JSON.stringify({
  510. id: 'local_ufrag',
  511. value: ufrag
  512. }));
  513. }
  514. });
  515. rtc.addListener(RTCEvents.REMOTE_UFRAG_CHANGED,
  516. (tpc, ufrag) => {
  517. if (!tpc.isP2P) {
  518. Statistics.sendLog(
  519. JSON.stringify({
  520. id: 'remote_ufrag',
  521. value: ufrag
  522. }));
  523. }
  524. });
  525. rtc.addListener(RTCEvents.CREATE_ANSWER_FAILED,
  526. (e, tpc) => {
  527. conference.statistics.sendCreateAnswerFailed(e, tpc);
  528. if (!tpc.isP2P) {
  529. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  530. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  531. }
  532. });
  533. rtc.addListener(RTCEvents.CREATE_OFFER_FAILED,
  534. (e, tpc) => {
  535. conference.statistics.sendCreateOfferFailed(e, tpc);
  536. if (!tpc.isP2P) {
  537. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  538. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  539. }
  540. });
  541. rtc.addListener(RTCEvents.SET_LOCAL_DESCRIPTION_FAILED,
  542. (e, tpc) => {
  543. conference.statistics.sendSetLocalDescFailed(e, tpc);
  544. if (!tpc.isP2P) {
  545. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  546. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  547. }
  548. });
  549. rtc.addListener(RTCEvents.SET_REMOTE_DESCRIPTION_FAILED,
  550. (e, tpc) => {
  551. conference.statistics.sendSetRemoteDescFailed(e, tpc);
  552. if (!tpc.isP2P) {
  553. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  554. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  555. }
  556. });
  557. rtc.addListener(RTCEvents.LOCAL_TRACK_SSRC_UPDATED,
  558. (track, ssrc) => {
  559. // when starting screen sharing, the track is created and when
  560. // we do set local description and we process the ssrc we
  561. // will be notified for it and we will report it with the event
  562. // for screen sharing
  563. if (track.isVideoTrack() && track.videoType === VideoType.DESKTOP) {
  564. conference.statistics.sendScreenSharingEvent(true, ssrc);
  565. }
  566. });
  567. };
  568. /**
  569. * Removes event listeners related to conference.xmpp
  570. */
  571. JitsiConferenceEventManager.prototype.removeXMPPListeners = function() {
  572. const conference = this.conference;
  573. Object.keys(this.xmppListeners).forEach(eventName => {
  574. conference.xmpp.removeListener(
  575. eventName,
  576. this.xmppListeners[eventName]);
  577. });
  578. this.xmppListeners = {};
  579. };
  580. /**
  581. * Setups event listeners related to conference.xmpp
  582. */
  583. JitsiConferenceEventManager.prototype.setupXMPPListeners = function() {
  584. const conference = this.conference;
  585. this._addConferenceXMPPListener(
  586. XMPPEvents.CALL_INCOMING,
  587. conference.onIncomingCall.bind(conference));
  588. this._addConferenceXMPPListener(
  589. XMPPEvents.CALL_ACCEPTED,
  590. conference.onCallAccepted.bind(conference));
  591. this._addConferenceXMPPListener(
  592. XMPPEvents.TRANSPORT_INFO,
  593. conference.onTransportInfo.bind(conference));
  594. this._addConferenceXMPPListener(
  595. XMPPEvents.CALL_ENDED,
  596. conference.onCallEnded.bind(conference));
  597. this._addConferenceXMPPListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  598. (audioMuted, videoMuted) => {
  599. if (conference.options.config.ignoreStartMuted) {
  600. return;
  601. }
  602. conference.startAudioMuted = audioMuted;
  603. conference.startVideoMuted = videoMuted;
  604. if (audioMuted) {
  605. conference.isMutedByFocus = true;
  606. }
  607. if (videoMuted) {
  608. conference.isVideoMutedByFocus = true;
  609. }
  610. // mute existing local tracks because this is initial mute from
  611. // Jicofo
  612. conference.getLocalTracks().forEach(track => {
  613. switch (track.getType()) {
  614. case MediaType.AUDIO:
  615. conference.startAudioMuted && track.mute();
  616. break;
  617. case MediaType.VIDEO:
  618. conference.startVideoMuted && track.mute();
  619. break;
  620. }
  621. });
  622. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  623. });
  624. this._addConferenceXMPPListener(XMPPEvents.CONFERENCE_TIMESTAMP_RECEIVED,
  625. createdTimestamp => {
  626. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_CREATED_TIMESTAMP, createdTimestamp);
  627. });
  628. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_CHANGED,
  629. (value, mediaType, actorJid) => {
  630. const actorParticipant = conference.getParticipants().find(p => p.getJid() === actorJid);
  631. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_CHANGED, {
  632. enabled: value,
  633. mediaType,
  634. actor: actorParticipant
  635. });
  636. });
  637. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_PARTICIPANT_APPROVED,
  638. (mediaType, jid) => {
  639. const participant = conference.getParticipantById(Strophe.getResourceFromJid(jid));
  640. if (participant) {
  641. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_PARTICIPANT_APPROVED, {
  642. participant,
  643. mediaType
  644. });
  645. }
  646. });
  647. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_PARTICIPANT_REJECTED,
  648. (mediaType, jid) => {
  649. const participant = conference.getParticipantById(Strophe.getResourceFromJid(jid));
  650. if (participant) {
  651. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_PARTICIPANT_REJECTED, {
  652. participant,
  653. mediaType
  654. });
  655. }
  656. });
  657. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_APPROVED,
  658. value => conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_APPROVED, { mediaType: value }));
  659. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_REJECTED,
  660. value => {
  661. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_REJECTED, { mediaType: value });
  662. });
  663. };
  664. /**
  665. * Add XMPP listener and save its reference for remove on leave conference.
  666. */
  667. JitsiConferenceEventManager.prototype._addConferenceXMPPListener = function(
  668. eventName, listener) {
  669. this.xmppListeners[eventName] = listener;
  670. this.conference.xmpp.addListener(eventName, listener);
  671. };
  672. /**
  673. * Setups event listeners related to conference.statistics
  674. */
  675. JitsiConferenceEventManager.prototype.setupStatisticsListeners = function() {
  676. const conference = this.conference;
  677. if (!conference.statistics) {
  678. return;
  679. }
  680. /* eslint-disable max-params */
  681. conference.statistics.addAudioLevelListener((tpc, ssrc, level, isLocal) => {
  682. conference.rtc.setAudioLevel(tpc, ssrc, level, isLocal);
  683. });
  684. /* eslint-enable max-params */
  685. // Forward the "before stats disposed" event
  686. conference.statistics.addBeforeDisposedListener(() => {
  687. conference.eventEmitter.emit(
  688. JitsiConferenceEvents.BEFORE_STATISTICS_DISPOSED);
  689. });
  690. // if we are in startSilent mode we will not be sending/receiving so nothing to detect
  691. if (!conference.options.config.startSilent) {
  692. conference.statistics.addByteSentStatsListener((tpc, stats) => {
  693. conference.getLocalTracks(MediaType.AUDIO).forEach(track => {
  694. const ssrc = tpc.getLocalSSRC(track);
  695. if (!ssrc || !stats.hasOwnProperty(ssrc)) {
  696. return;
  697. }
  698. track.onByteSentStatsReceived(tpc, stats[ssrc]);
  699. });
  700. });
  701. }
  702. };