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

JitsiConferenceEventManager.js 20KB

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