Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

JitsiConferenceEventManager.js 31KB

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