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

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