您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

JitsiConferenceEventManager.js 29KB

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