Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

JitsiConferenceEventManager.js 29KB

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