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

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