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

JitsiConferenceEventManager.js 30KB

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