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

JitsiConferenceEventManager.js 28KB

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