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

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