You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JitsiConferenceEventManager.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /* global __filename, Strophe */
  2. import AuthenticationEvents
  3. from './service/authentication/AuthenticationEvents';
  4. import EventEmitterForwarder from './modules/util/EventEmitterForwarder';
  5. import { getLogger } from 'jitsi-meet-logger';
  6. import * as JitsiConferenceErrors from './JitsiConferenceErrors';
  7. import * as JitsiConferenceEvents from './JitsiConferenceEvents';
  8. import * as MediaType from './service/RTC/MediaType';
  9. import RTCEvents from './service/RTC/RTCEvents';
  10. import Statistics from './modules/statistics/statistics';
  11. import XMPPEvents from './service/xmpp/XMPPEvents';
  12. const logger = getLogger(__filename);
  13. /**
  14. * Setups all event listeners related to conference
  15. * @param conference {JitsiConference} the conference
  16. */
  17. export default function JitsiConferenceEventManager(conference) {
  18. this.conference = conference;
  19. // Listeners related to the conference only
  20. conference.on(JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  21. track => {
  22. if (!track.isLocal() || !conference.statistics) {
  23. return;
  24. }
  25. const session
  26. = track.isP2P
  27. ? conference.p2pJingleSession : conference.jvbJingleSession;
  28. // TPC will be null, before the conference starts, but the event
  29. // still should be queued
  30. const tpc = (session && session.peerconnection) || null;
  31. conference.statistics.sendMuteEvent(
  32. tpc,
  33. track.isMuted(),
  34. track.getType());
  35. });
  36. conference.on(
  37. JitsiConferenceEvents.CONNECTION_INTERRUPTED,
  38. Statistics.sendEventToAll.bind(Statistics, 'connection.interrupted'));
  39. conference.on(
  40. JitsiConferenceEvents.CONNECTION_RESTORED,
  41. Statistics.sendEventToAll.bind(Statistics, 'connection.restored'));
  42. }
  43. /**
  44. * Setups event listeners related to conference.chatRoom
  45. */
  46. JitsiConferenceEventManager.prototype.setupChatRoomListeners = function() {
  47. const conference = this.conference;
  48. const chatRoom = conference.room;
  49. this.chatRoomForwarder = new EventEmitterForwarder(chatRoom,
  50. this.conference.eventEmitter);
  51. chatRoom.addListener(XMPPEvents.ICE_RESTARTING, jingleSession => {
  52. if (!jingleSession.isP2P) {
  53. // All data channels have to be closed, before ICE restart
  54. // otherwise Chrome will not trigger "opened" event for the channel
  55. // established with the new bridge
  56. conference.rtc.closeAllDataChannels();
  57. }
  58. // else: there are no DataChannels in P2P session (at least for now)
  59. });
  60. chatRoom.addListener(XMPPEvents.AUDIO_MUTED_BY_FOCUS,
  61. value => {
  62. // set isMutedByFocus when setAudioMute Promise ends
  63. conference.rtc.setAudioMute(value).then(
  64. () => {
  65. conference.isMutedByFocus = true;
  66. },
  67. () =>
  68. logger.warn(
  69. 'Error while audio muting due to focus request'));
  70. }
  71. );
  72. this.chatRoomForwarder.forward(XMPPEvents.SUBJECT_CHANGED,
  73. JitsiConferenceEvents.SUBJECT_CHANGED);
  74. this.chatRoomForwarder.forward(XMPPEvents.MUC_JOINED,
  75. JitsiConferenceEvents.CONFERENCE_JOINED);
  76. // send some analytics events
  77. chatRoom.addListener(XMPPEvents.MUC_JOINED,
  78. () => {
  79. this.conference.isJvbConnectionInterrupted = false;
  80. Object.keys(chatRoom.connectionTimes).forEach(key => {
  81. const value = chatRoom.connectionTimes[key];
  82. Statistics.analytics.sendEvent(`conference.${key}`, { value });
  83. });
  84. Object.keys(chatRoom.xmpp.connectionTimes).forEach(key => {
  85. const value = chatRoom.xmpp.connectionTimes[key];
  86. Statistics.analytics.sendEvent(`xmpp.${key}`, { value });
  87. });
  88. });
  89. this.chatRoomForwarder.forward(XMPPEvents.ROOM_JOIN_ERROR,
  90. JitsiConferenceEvents.CONFERENCE_FAILED,
  91. JitsiConferenceErrors.CONNECTION_ERROR);
  92. this.chatRoomForwarder.forward(XMPPEvents.ROOM_CONNECT_ERROR,
  93. JitsiConferenceEvents.CONFERENCE_FAILED,
  94. JitsiConferenceErrors.CONNECTION_ERROR);
  95. this.chatRoomForwarder.forward(XMPPEvents.ROOM_CONNECT_NOT_ALLOWED_ERROR,
  96. JitsiConferenceEvents.CONFERENCE_FAILED,
  97. JitsiConferenceErrors.NOT_ALLOWED_ERROR);
  98. this.chatRoomForwarder.forward(XMPPEvents.ROOM_MAX_USERS_ERROR,
  99. JitsiConferenceEvents.CONFERENCE_FAILED,
  100. JitsiConferenceErrors.CONFERENCE_MAX_USERS);
  101. this.chatRoomForwarder.forward(XMPPEvents.PASSWORD_REQUIRED,
  102. JitsiConferenceEvents.CONFERENCE_FAILED,
  103. JitsiConferenceErrors.PASSWORD_REQUIRED);
  104. this.chatRoomForwarder.forward(XMPPEvents.AUTHENTICATION_REQUIRED,
  105. JitsiConferenceEvents.CONFERENCE_FAILED,
  106. JitsiConferenceErrors.AUTHENTICATION_REQUIRED);
  107. this.chatRoomForwarder.forward(XMPPEvents.BRIDGE_DOWN,
  108. JitsiConferenceEvents.CONFERENCE_FAILED,
  109. JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  110. chatRoom.addListener(
  111. XMPPEvents.BRIDGE_DOWN,
  112. () => Statistics.analytics.sendEvent('conference.bridgeDown'));
  113. this.chatRoomForwarder.forward(XMPPEvents.RESERVATION_ERROR,
  114. JitsiConferenceEvents.CONFERENCE_FAILED,
  115. JitsiConferenceErrors.RESERVATION_ERROR);
  116. this.chatRoomForwarder.forward(XMPPEvents.GRACEFUL_SHUTDOWN,
  117. JitsiConferenceEvents.CONFERENCE_FAILED,
  118. JitsiConferenceErrors.GRACEFUL_SHUTDOWN);
  119. chatRoom.addListener(XMPPEvents.JINGLE_FATAL_ERROR,
  120. (session, error) => {
  121. if (!session.isP2P) {
  122. conference.eventEmitter.emit(
  123. JitsiConferenceEvents.CONFERENCE_FAILED,
  124. JitsiConferenceErrors.JINGLE_FATAL_ERROR, error);
  125. }
  126. });
  127. chatRoom.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
  128. jingleSession => {
  129. conference._onIceConnectionFailed(jingleSession);
  130. });
  131. this.chatRoomForwarder.forward(XMPPEvents.MUC_DESTROYED,
  132. JitsiConferenceEvents.CONFERENCE_FAILED,
  133. JitsiConferenceErrors.CONFERENCE_DESTROYED);
  134. this.chatRoomForwarder.forward(XMPPEvents.CHAT_ERROR_RECEIVED,
  135. JitsiConferenceEvents.CONFERENCE_ERROR,
  136. JitsiConferenceErrors.CHAT_ERROR);
  137. this.chatRoomForwarder.forward(XMPPEvents.FOCUS_DISCONNECTED,
  138. JitsiConferenceEvents.CONFERENCE_FAILED,
  139. JitsiConferenceErrors.FOCUS_DISCONNECTED);
  140. chatRoom.addListener(XMPPEvents.FOCUS_LEFT,
  141. () => {
  142. Statistics.analytics.sendEvent('conference.focusLeft');
  143. conference.eventEmitter.emit(
  144. JitsiConferenceEvents.CONFERENCE_FAILED,
  145. JitsiConferenceErrors.FOCUS_LEFT);
  146. });
  147. const eventLogHandler
  148. = reason => Statistics.sendEventToAll(`conference.error.${reason}`);
  149. chatRoom.addListener(XMPPEvents.SESSION_ACCEPT_TIMEOUT,
  150. jingleSession => {
  151. eventLogHandler(
  152. jingleSession.isP2P
  153. ? 'p2pSessionAcceptTimeout' : 'sessionAcceptTimeout');
  154. });
  155. this.chatRoomForwarder.forward(XMPPEvents.RECORDER_STATE_CHANGED,
  156. JitsiConferenceEvents.RECORDER_STATE_CHANGED);
  157. this.chatRoomForwarder.forward(XMPPEvents.VIDEO_SIP_GW_AVAILABILITY_CHANGED,
  158. JitsiConferenceEvents.VIDEO_SIP_GW_AVAILABILITY_CHANGED);
  159. this.chatRoomForwarder.forward(XMPPEvents.PHONE_NUMBER_CHANGED,
  160. JitsiConferenceEvents.PHONE_NUMBER_CHANGED);
  161. chatRoom.addListener(
  162. XMPPEvents.CONFERENCE_SETUP_FAILED,
  163. (jingleSession, error) => {
  164. if (!jingleSession.isP2P) {
  165. conference.eventEmitter.emit(
  166. JitsiConferenceEvents.CONFERENCE_FAILED,
  167. JitsiConferenceErrors.SETUP_FAILED,
  168. error);
  169. }
  170. });
  171. chatRoom.setParticipantPropertyListener((node, from) => {
  172. const participant = conference.getParticipantById(from);
  173. if (!participant) {
  174. return;
  175. }
  176. participant.setProperty(
  177. node.tagName.substring('jitsi_participant_'.length),
  178. node.value);
  179. });
  180. this.chatRoomForwarder.forward(XMPPEvents.KICKED,
  181. JitsiConferenceEvents.KICKED);
  182. chatRoom.addListener(XMPPEvents.KICKED,
  183. () => {
  184. conference.room = null;
  185. conference.leave();
  186. });
  187. chatRoom.addListener(XMPPEvents.SUSPEND_DETECTED,
  188. conference.onSuspendDetected.bind(conference));
  189. this.chatRoomForwarder.forward(XMPPEvents.MUC_LOCK_CHANGED,
  190. JitsiConferenceEvents.LOCK_STATE_CHANGED);
  191. chatRoom.addListener(XMPPEvents.MUC_MEMBER_JOINED,
  192. conference.onMemberJoined.bind(conference));
  193. chatRoom.addListener(XMPPEvents.MUC_MEMBER_LEFT,
  194. conference.onMemberLeft.bind(conference));
  195. this.chatRoomForwarder.forward(XMPPEvents.MUC_LEFT,
  196. JitsiConferenceEvents.CONFERENCE_LEFT);
  197. chatRoom.addListener(XMPPEvents.DISPLAY_NAME_CHANGED,
  198. conference.onDisplayNameChanged.bind(conference));
  199. chatRoom.addListener(XMPPEvents.LOCAL_ROLE_CHANGED, role => {
  200. conference.onLocalRoleChanged(role);
  201. // log all events for the recorder operated by the moderator
  202. if (conference.statistics && conference.isModerator()) {
  203. conference.on(JitsiConferenceEvents.RECORDER_STATE_CHANGED,
  204. (status, error) => {
  205. const logObject = {
  206. id: 'recorder_status',
  207. status
  208. };
  209. if (error) {
  210. logObject.error = error;
  211. }
  212. Statistics.sendLog(JSON.stringify(logObject));
  213. });
  214. }
  215. });
  216. chatRoom.addListener(XMPPEvents.MUC_ROLE_CHANGED,
  217. conference.onUserRoleChanged.bind(conference));
  218. chatRoom.addListener(AuthenticationEvents.IDENTITY_UPDATED,
  219. (authEnabled, authIdentity) => {
  220. conference.authEnabled = authEnabled;
  221. conference.authIdentity = authIdentity;
  222. conference.eventEmitter.emit(
  223. JitsiConferenceEvents.AUTH_STATUS_CHANGED, authEnabled,
  224. authIdentity);
  225. });
  226. chatRoom.addListener(
  227. XMPPEvents.MESSAGE_RECEIVED,
  228. // eslint-disable-next-line max-params
  229. (jid, displayName, txt, myJid, ts) => {
  230. const id = Strophe.getResourceFromJid(jid);
  231. conference.eventEmitter.emit(
  232. JitsiConferenceEvents.MESSAGE_RECEIVED,
  233. id, txt, ts);
  234. });
  235. chatRoom.addListener(XMPPEvents.PRESENCE_STATUS,
  236. (jid, status) => {
  237. const id = Strophe.getResourceFromJid(jid);
  238. const participant = conference.getParticipantById(id);
  239. if (!participant || participant._status === status) {
  240. return;
  241. }
  242. participant._status = status;
  243. conference.eventEmitter.emit(
  244. JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
  245. });
  246. chatRoom.addPresenceListener('startmuted', (data, from) => {
  247. let isModerator = false;
  248. if (conference.myUserId() === from && conference.isModerator()) {
  249. isModerator = true;
  250. } else {
  251. const participant = conference.getParticipantById(from);
  252. if (participant && participant.isModerator()) {
  253. isModerator = true;
  254. }
  255. }
  256. if (!isModerator) {
  257. return;
  258. }
  259. const startAudioMuted = data.attributes.audio === 'true';
  260. const startVideoMuted = data.attributes.video === 'true';
  261. let updated = false;
  262. if (startAudioMuted !== conference.startMutedPolicy.audio) {
  263. conference.startMutedPolicy.audio = startAudioMuted;
  264. updated = true;
  265. }
  266. if (startVideoMuted !== conference.startMutedPolicy.video) {
  267. conference.startMutedPolicy.video = startVideoMuted;
  268. updated = true;
  269. }
  270. if (updated) {
  271. conference.eventEmitter.emit(
  272. JitsiConferenceEvents.START_MUTED_POLICY_CHANGED,
  273. conference.startMutedPolicy
  274. );
  275. }
  276. });
  277. chatRoom.addPresenceListener('devices', (data, from) => {
  278. let isAudioAvailable = false;
  279. let isVideoAvailable = false;
  280. data.children.forEach(config => {
  281. if (config.tagName === 'audio') {
  282. isAudioAvailable = config.value === 'true';
  283. }
  284. if (config.tagName === 'video') {
  285. isVideoAvailable = config.value === 'true';
  286. }
  287. });
  288. let availableDevices;
  289. if (conference.myUserId() === from) {
  290. availableDevices = conference.availableDevices;
  291. } else {
  292. const participant = conference.getParticipantById(from);
  293. if (!participant) {
  294. return;
  295. }
  296. availableDevices = participant._availableDevices;
  297. }
  298. let updated = false;
  299. if (availableDevices.audio !== isAudioAvailable) {
  300. updated = true;
  301. availableDevices.audio = isAudioAvailable;
  302. }
  303. if (availableDevices.video !== isVideoAvailable) {
  304. updated = true;
  305. availableDevices.video = isVideoAvailable;
  306. }
  307. if (updated) {
  308. conference.eventEmitter.emit(
  309. JitsiConferenceEvents.AVAILABLE_DEVICES_CHANGED,
  310. from, availableDevices);
  311. }
  312. });
  313. if (conference.statistics) {
  314. // FIXME ICE related events should end up in RTCEvents eventually
  315. chatRoom.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
  316. session => {
  317. conference.statistics.sendIceConnectionFailedEvent(
  318. session.peerconnection);
  319. });
  320. // FIXME XMPPEvents.ADD_ICE_CANDIDATE_FAILED is never emitted
  321. chatRoom.addListener(XMPPEvents.ADD_ICE_CANDIDATE_FAILED,
  322. (e, pc) => {
  323. conference.statistics.sendAddIceCandidateFailed(e, pc);
  324. });
  325. }
  326. };
  327. /**
  328. * Setups event listeners related to conference.rtc
  329. */
  330. JitsiConferenceEventManager.prototype.setupRTCListeners = function() {
  331. const conference = this.conference;
  332. const rtc = conference.rtc;
  333. rtc.addListener(
  334. RTCEvents.REMOTE_TRACK_ADDED,
  335. conference.onRemoteTrackAdded.bind(conference));
  336. rtc.addListener(
  337. RTCEvents.REMOTE_TRACK_REMOVED,
  338. conference.onRemoteTrackRemoved.bind(conference));
  339. rtc.addListener(RTCEvents.DOMINANT_SPEAKER_CHANGED,
  340. id => {
  341. if (conference.lastDominantSpeaker !== id && conference.room) {
  342. conference.lastDominantSpeaker = id;
  343. conference.eventEmitter.emit(
  344. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED, id);
  345. }
  346. if (conference.statistics && conference.myUserId() === id) {
  347. // We are the new dominant speaker.
  348. conference.statistics.sendDominantSpeakerEvent();
  349. }
  350. });
  351. rtc.addListener(RTCEvents.DATA_CHANNEL_OPEN, () => {
  352. const now = window.performance.now();
  353. logger.log('(TIME) data channel opened ', now);
  354. conference.room.connectionTimes['data.channel.opened'] = now;
  355. Statistics.analytics.sendEvent('conference.dataChannel.open',
  356. { value: now });
  357. });
  358. rtc.addListener(
  359. RTCEvents.AVAILABLE_DEVICES_CHANGED,
  360. devices => conference.room.updateDeviceAvailability(devices));
  361. rtc.addListener(RTCEvents.ENDPOINT_MESSAGE_RECEIVED,
  362. (from, payload) => {
  363. const participant = conference.getParticipantById(from);
  364. if (participant) {
  365. conference.eventEmitter.emit(
  366. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  367. participant, payload);
  368. } else {
  369. logger.warn(
  370. 'Ignored ENDPOINT_MESSAGE_RECEIVED for not existing '
  371. + `participant: ${from}`,
  372. payload);
  373. }
  374. });
  375. rtc.addListener(RTCEvents.LOCAL_UFRAG_CHANGED,
  376. (tpc, ufrag) => {
  377. if (!tpc.isP2P) {
  378. Statistics.sendLog(
  379. JSON.stringify({
  380. id: 'local_ufrag',
  381. value: ufrag
  382. }));
  383. }
  384. });
  385. rtc.addListener(RTCEvents.REMOTE_UFRAG_CHANGED,
  386. (tpc, ufrag) => {
  387. if (!tpc.isP2P) {
  388. Statistics.sendLog(
  389. JSON.stringify({
  390. id: 'remote_ufrag',
  391. value: ufrag
  392. }));
  393. }
  394. });
  395. if (conference.statistics) {
  396. rtc.addListener(RTCEvents.CREATE_ANSWER_FAILED,
  397. (e, tpc) => {
  398. conference.statistics.sendCreateAnswerFailed(e, tpc);
  399. });
  400. rtc.addListener(RTCEvents.CREATE_OFFER_FAILED,
  401. (e, tpc) => {
  402. conference.statistics.sendCreateOfferFailed(e, tpc);
  403. });
  404. rtc.addListener(RTCEvents.SET_LOCAL_DESCRIPTION_FAILED,
  405. (e, tpc) => {
  406. conference.statistics.sendSetLocalDescFailed(e, tpc);
  407. });
  408. rtc.addListener(RTCEvents.SET_REMOTE_DESCRIPTION_FAILED,
  409. (e, tpc) => {
  410. conference.statistics.sendSetRemoteDescFailed(e, tpc);
  411. });
  412. }
  413. };
  414. /**
  415. * Setups event listeners related to conference.xmpp
  416. */
  417. JitsiConferenceEventManager.prototype.setupXMPPListeners = function() {
  418. const conference = this.conference;
  419. conference.xmpp.caps.addListener(XMPPEvents.PARTCIPANT_FEATURES_CHANGED,
  420. from => {
  421. const participant
  422. = conference.getParticipantId(
  423. Strophe.getResourceFromJid(from));
  424. if (participant) {
  425. conference.eventEmitter.emit(
  426. JitsiConferenceEvents.PARTCIPANT_FEATURES_CHANGED,
  427. participant);
  428. }
  429. });
  430. conference.xmpp.addListener(
  431. XMPPEvents.CALL_INCOMING,
  432. conference.onIncomingCall.bind(conference));
  433. conference.xmpp.addListener(
  434. XMPPEvents.CALL_ACCEPTED,
  435. conference.onCallAccepted.bind(conference));
  436. conference.xmpp.addListener(
  437. XMPPEvents.TRANSPORT_INFO,
  438. conference.onTransportInfo.bind(conference));
  439. conference.xmpp.addListener(
  440. XMPPEvents.CALL_ENDED,
  441. conference.onCallEnded.bind(conference));
  442. conference.xmpp.addListener(XMPPEvents.START_MUTED_FROM_FOCUS,
  443. (audioMuted, videoMuted) => {
  444. if (conference.options.config.ignoreStartMuted) {
  445. return;
  446. }
  447. conference.startAudioMuted = audioMuted;
  448. conference.startVideoMuted = videoMuted;
  449. // mute existing local tracks because this is initial mute from
  450. // Jicofo
  451. conference.getLocalTracks().forEach(track => {
  452. switch (track.getType()) {
  453. case MediaType.AUDIO:
  454. conference.startAudioMuted && track.mute();
  455. break;
  456. case MediaType.VIDEO:
  457. conference.startVideoMuted && track.mute();
  458. break;
  459. }
  460. });
  461. conference.eventEmitter.emit(JitsiConferenceEvents.STARTED_MUTED);
  462. });
  463. };
  464. /**
  465. * Setups event listeners related to conference.statistics
  466. */
  467. JitsiConferenceEventManager.prototype.setupStatisticsListeners = function() {
  468. const conference = this.conference;
  469. if (!conference.statistics) {
  470. return;
  471. }
  472. /* eslint-disable max-params */
  473. conference.statistics.addAudioLevelListener((tpc, ssrc, level, isLocal) => {
  474. conference.rtc.setAudioLevel(tpc, ssrc, level, isLocal);
  475. });
  476. /* eslint-enable max-params */
  477. // Forward the "before stats disposed" event
  478. conference.statistics.addBeforeDisposedListener(() => {
  479. conference.eventEmitter.emit(
  480. JitsiConferenceEvents.BEFORE_STATISTICS_DISPOSED);
  481. });
  482. conference.statistics.addByteSentStatsListener((tpc, stats) => {
  483. conference.getLocalTracks(MediaType.AUDIO).forEach(track => {
  484. const ssrc = tpc.getLocalSSRC(track);
  485. if (!ssrc || !stats.hasOwnProperty(ssrc)) {
  486. return;
  487. }
  488. track._setByteSent(tpc, stats[ssrc]);
  489. });
  490. });
  491. };