Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

JitsiConferenceEventManager.js 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. };
  409. /**
  410. * Setups event listeners related to conference.rtc
  411. */
  412. JitsiConferenceEventManager.prototype.setupRTCListeners = function() {
  413. const conference = this.conference;
  414. const rtc = conference.rtc;
  415. rtc.addListener(
  416. RTCEvents.REMOTE_TRACK_ADDED,
  417. conference.onRemoteTrackAdded.bind(conference));
  418. rtc.addListener(
  419. RTCEvents.REMOTE_TRACK_REMOVED,
  420. conference.onRemoteTrackRemoved.bind(conference));
  421. rtc.addListener(RTCEvents.DOMINANT_SPEAKER_CHANGED,
  422. (dominant, previous) => {
  423. if (conference.lastDominantSpeaker !== dominant && conference.room) {
  424. conference.lastDominantSpeaker = dominant;
  425. conference.eventEmitter.emit(
  426. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, dominant, previous);
  427. if (previous && previous.length) {
  428. const speakerList = previous.slice(0);
  429. // Add the dominant speaker to the top of the list (exclude self).
  430. if (conference.myUserId !== dominant) {
  431. speakerList.splice(0, 0, dominant);
  432. }
  433. // Trim the list to the top 5 speakers only.
  434. if (speakerList.length > SPEAKERS_AUDIO_LEVELS) {
  435. speakerList.splice(SPEAKERS_AUDIO_LEVELS, speakerList.length - SPEAKERS_AUDIO_LEVELS);
  436. }
  437. conference.statistics && conference.statistics.setSpeakerList(speakerList);
  438. }
  439. if (conference.statistics && conference.myUserId() === dominant) {
  440. // We are the new dominant speaker.
  441. conference.statistics.sendDominantSpeakerEvent(conference.room.roomjid);
  442. }
  443. }
  444. });
  445. rtc.addListener(RTCEvents.DATA_CHANNEL_OPEN, () => {
  446. const now = window.performance.now();
  447. const key = 'data.channel.opened';
  448. // TODO: Move all of the 'connectionTimes' logic to its own module.
  449. logger.log(`(TIME) ${key}:\t`, now);
  450. conference.room.connectionTimes[key] = now;
  451. Statistics.sendAnalytics(
  452. createConnectionStageReachedEvent(key, { value: now }));
  453. conference.eventEmitter.emit(JitsiConferenceEvents.DATA_CHANNEL_OPENED);
  454. });
  455. rtc.addListener(RTCEvents.ENDPOINT_MESSAGE_RECEIVED,
  456. (from, payload) => {
  457. const participant = conference.getParticipantById(from);
  458. if (participant) {
  459. conference.eventEmitter.emit(
  460. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  461. participant, payload);
  462. } else {
  463. logger.warn(
  464. 'Ignored ENDPOINT_MESSAGE_RECEIVED for not existing '
  465. + `participant: ${from}`,
  466. payload);
  467. }
  468. });
  469. rtc.addListener(RTCEvents.ENDPOINT_STATS_RECEIVED,
  470. (from, payload) => {
  471. const participant = conference.getParticipantById(from);
  472. if (participant) {
  473. conference.eventEmitter.emit(JitsiConferenceEvents.ENDPOINT_STATS_RECEIVED, participant, payload);
  474. } else {
  475. logger.warn(`Ignoring ENDPOINT_STATS_RECEIVED for a non-existant participant: ${from}`);
  476. }
  477. });
  478. rtc.addListener(RTCEvents.LOCAL_UFRAG_CHANGED,
  479. (tpc, ufrag) => {
  480. if (!tpc.isP2P) {
  481. Statistics.sendLog(
  482. JSON.stringify({
  483. id: 'local_ufrag',
  484. value: ufrag
  485. }));
  486. }
  487. });
  488. rtc.addListener(RTCEvents.REMOTE_UFRAG_CHANGED,
  489. (tpc, ufrag) => {
  490. if (!tpc.isP2P) {
  491. Statistics.sendLog(
  492. JSON.stringify({
  493. id: 'remote_ufrag',
  494. value: ufrag
  495. }));
  496. }
  497. });
  498. rtc.addListener(RTCEvents.CREATE_ANSWER_FAILED,
  499. (e, tpc) => {
  500. conference.statistics.sendCreateAnswerFailed(e, tpc);
  501. if (!tpc.isP2P) {
  502. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  503. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  504. }
  505. });
  506. rtc.addListener(RTCEvents.CREATE_OFFER_FAILED,
  507. (e, tpc) => {
  508. conference.statistics.sendCreateOfferFailed(e, tpc);
  509. if (!tpc.isP2P) {
  510. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  511. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  512. }
  513. });
  514. rtc.addListener(RTCEvents.SET_LOCAL_DESCRIPTION_FAILED,
  515. (e, tpc) => {
  516. conference.statistics.sendSetLocalDescFailed(e, tpc);
  517. if (!tpc.isP2P) {
  518. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  519. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  520. }
  521. });
  522. rtc.addListener(RTCEvents.SET_REMOTE_DESCRIPTION_FAILED,
  523. (e, tpc) => {
  524. conference.statistics.sendSetRemoteDescFailed(e, tpc);
  525. if (!tpc.isP2P) {
  526. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_FAILED,
  527. JitsiConferenceErrors.OFFER_ANSWER_FAILED, e);
  528. }
  529. });
  530. rtc.addListener(RTCEvents.LOCAL_TRACK_SSRC_UPDATED,
  531. (track, ssrc) => {
  532. // when starting screen sharing, the track is created and when
  533. // we do set local description and we process the ssrc we
  534. // will be notified for it and we will report it with the event
  535. // for screen sharing
  536. if (track.isVideoTrack() && track.videoType === VideoType.DESKTOP) {
  537. conference.statistics.sendScreenSharingEvent(true, ssrc);
  538. }
  539. });
  540. };
  541. /**
  542. * Removes event listeners related to conference.xmpp
  543. */
  544. JitsiConferenceEventManager.prototype.removeXMPPListeners = function() {
  545. const conference = this.conference;
  546. Object.keys(this.xmppListeners).forEach(eventName => {
  547. conference.xmpp.removeListener(
  548. eventName,
  549. this.xmppListeners[eventName]);
  550. });
  551. this.xmppListeners = {};
  552. };
  553. /**
  554. * Setups event listeners related to conference.xmpp
  555. */
  556. JitsiConferenceEventManager.prototype.setupXMPPListeners = function() {
  557. const conference = this.conference;
  558. this._addConferenceXMPPListener(
  559. XMPPEvents.CALL_INCOMING,
  560. conference.onIncomingCall.bind(conference));
  561. this._addConferenceXMPPListener(
  562. XMPPEvents.CALL_ACCEPTED,
  563. conference.onCallAccepted.bind(conference));
  564. this._addConferenceXMPPListener(
  565. XMPPEvents.TRANSPORT_INFO,
  566. conference.onTransportInfo.bind(conference));
  567. this._addConferenceXMPPListener(
  568. XMPPEvents.CALL_ENDED,
  569. conference.onCallEnded.bind(conference));
  570. this._addConferenceXMPPListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  571. (audioMuted, videoMuted) => {
  572. if (conference.options.config.ignoreStartMuted) {
  573. return;
  574. }
  575. conference.startAudioMuted = audioMuted;
  576. conference.startVideoMuted = videoMuted;
  577. // mute existing local tracks because this is initial mute from
  578. // Jicofo
  579. conference.getLocalTracks().forEach(track => {
  580. switch (track.getType()) {
  581. case MediaType.AUDIO:
  582. conference.startAudioMuted && track.mute();
  583. break;
  584. case MediaType.VIDEO:
  585. conference.startVideoMuted && track.mute();
  586. break;
  587. }
  588. });
  589. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  590. });
  591. this._addConferenceXMPPListener(XMPPEvents.CONFERENCE_TIMESTAMP_RECEIVED,
  592. createdTimestamp => {
  593. conference.eventEmitter.emit(JitsiConferenceEvents.CONFERENCE_CREATED_TIMESTAMP, createdTimestamp);
  594. });
  595. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_CHANGED,
  596. (value, mediaType, actorJid) => {
  597. const actorParticipant = conference.getParticipants().find(p => p.getJid() === actorJid);
  598. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_CHANGED, {
  599. enabled: value,
  600. mediaType,
  601. actor: actorParticipant
  602. });
  603. });
  604. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_PARTICIPANT_APPROVED,
  605. (mediaType, jid) => {
  606. const participant = conference.getParticipantById(Strophe.getResourceFromJid(jid));
  607. if (participant) {
  608. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_PARTICIPANT_APPROVED, {
  609. participant,
  610. mediaType
  611. });
  612. }
  613. });
  614. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_PARTICIPANT_REJECTED,
  615. (mediaType, jid) => {
  616. const participant = conference.getParticipantById(Strophe.getResourceFromJid(jid));
  617. if (participant) {
  618. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_PARTICIPANT_REJECTED, {
  619. participant,
  620. mediaType
  621. });
  622. }
  623. });
  624. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_APPROVED,
  625. value => conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_APPROVED, { mediaType: value }));
  626. this._addConferenceXMPPListener(XMPPEvents.AV_MODERATION_REJECTED,
  627. value => {
  628. conference.eventEmitter.emit(JitsiConferenceEvents.AV_MODERATION_REJECTED, { mediaType: value });
  629. });
  630. };
  631. /**
  632. * Add XMPP listener and save its reference for remove on leave conference.
  633. */
  634. JitsiConferenceEventManager.prototype._addConferenceXMPPListener = function(
  635. eventName, listener) {
  636. this.xmppListeners[eventName] = listener;
  637. this.conference.xmpp.addListener(eventName, listener);
  638. };
  639. /**
  640. * Setups event listeners related to conference.statistics
  641. */
  642. JitsiConferenceEventManager.prototype.setupStatisticsListeners = function() {
  643. const conference = this.conference;
  644. if (!conference.statistics) {
  645. return;
  646. }
  647. /* eslint-disable max-params */
  648. conference.statistics.addAudioLevelListener((tpc, ssrc, level, isLocal) => {
  649. conference.rtc.setAudioLevel(tpc, ssrc, level, isLocal);
  650. });
  651. /* eslint-enable max-params */
  652. // Forward the "before stats disposed" event
  653. conference.statistics.addBeforeDisposedListener(() => {
  654. conference.eventEmitter.emit(
  655. JitsiConferenceEvents.BEFORE_STATISTICS_DISPOSED);
  656. });
  657. // if we are in startSilent mode we will not be sending/receiving so nothing to detect
  658. if (!conference.options.config.startSilent) {
  659. conference.statistics.addByteSentStatsListener((tpc, stats) => {
  660. conference.getLocalTracks(MediaType.AUDIO).forEach(track => {
  661. const ssrc = tpc.getLocalSSRC(track);
  662. if (!ssrc || !stats.hasOwnProperty(ssrc)) {
  663. return;
  664. }
  665. track.onByteSentStatsReceived(tpc, stats[ssrc]);
  666. });
  667. });
  668. }
  669. };