modified lib-jitsi-meet dev repo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

e2eping.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* global __filename */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  4. import * as E2ePingEvents
  5. from '../../service/e2eping/E2ePingEvents';
  6. import { createE2eRttEvent } from '../../service/statistics/AnalyticsEvents';
  7. import Statistics from '../statistics/statistics';
  8. const logger = getLogger(__filename);
  9. /**
  10. * The 'type' of a message which designates an e2e ping request.
  11. * @type {string}
  12. */
  13. const E2E_PING_REQUEST = 'e2e-ping-request';
  14. /**
  15. * The 'type' of a message which designates an e2e ping response.
  16. * @type {string}
  17. */
  18. const E2E_PING_RESPONSE = 'e2e-ping-response';
  19. /**
  20. * Saves e2e ping related state for a single JitsiParticipant.
  21. */
  22. class ParticipantWrapper {
  23. /**
  24. * Creates a ParticipantWrapper
  25. * @param {JitsiParticipant} participant - The remote participant that this
  26. * object wraps.
  27. * @param {E2ePing} e2eping
  28. */
  29. constructor(participant, e2eping) {
  30. // The JitsiParticipant
  31. this.participant = participant;
  32. // The E2ePing
  33. this.e2eping = e2eping;
  34. // Caches the ID
  35. this.id = participant.getId();
  36. // Recently sent requests
  37. this.requests = {};
  38. // The ID of the last sent request. We just increment it for each new
  39. // request. Start at 1 so we can consider only thruthy values valid.
  40. this.lastRequestId = 1;
  41. this.clearIntervals = this.clearIntervals.bind(this);
  42. this.sendRequest = this.sendRequest.bind(this);
  43. this.handleResponse = this.handleResponse.bind(this);
  44. this.maybeSendAnalytics = this.maybeSendAnalytics.bind(this);
  45. this.sendAnalytics = this.sendAnalytics.bind(this);
  46. // If the data channel was already open (this is likely a participant
  47. // joining an existing conference) send a request immediately.
  48. if (e2eping.isDataChannelOpen) {
  49. this.sendRequest();
  50. }
  51. this.pingInterval = window.setInterval(
  52. this.sendRequest, e2eping.pingIntervalMs);
  53. this.analyticsInterval = window.setTimeout(
  54. this.maybeSendAnalytics, this.e2eping.analyticsIntervalMs);
  55. }
  56. /**
  57. * Clears the interval which sends pings.
  58. * @type {*}
  59. */
  60. clearIntervals() {
  61. if (this.pingInterval) {
  62. window.clearInterval(this.pingInterval);
  63. }
  64. if (this.analyticsInterval) {
  65. window.clearInterval(this.analyticsInterval);
  66. }
  67. }
  68. /**
  69. * Sends the next ping request.
  70. * @type {*}
  71. */
  72. sendRequest() {
  73. const requestId = this.lastRequestId++;
  74. const requestMessage = {
  75. type: E2E_PING_REQUEST,
  76. id: requestId
  77. };
  78. this.e2eping.sendMessage(requestMessage, this.id);
  79. this.requests[requestId] = {
  80. id: requestId,
  81. timeSent: window.performance.now()
  82. };
  83. }
  84. /**
  85. * Handles a response from this participant.
  86. * @type {*}
  87. */
  88. handleResponse(response) {
  89. const request = this.requests[response.id];
  90. if (request) {
  91. request.rtt = window.performance.now() - request.timeSent;
  92. this.e2eping.eventEmitter.emit(
  93. E2ePingEvents.E2E_RTT_CHANGED,
  94. this.participant,
  95. request.rtt);
  96. }
  97. this.maybeSendAnalytics();
  98. }
  99. /**
  100. * Goes over the requests, clearing ones which we don't need anymore, and
  101. * if it finds at least one request with a valid RTT in the last
  102. * 'analyticsIntervalMs' then sends an analytics event.
  103. * @type {*}
  104. */
  105. maybeSendAnalytics() {
  106. const now = window.performance.now();
  107. // The RTT we'll report is the minimum RTT measured in the last
  108. // analyticsInterval
  109. let rtt = Infinity;
  110. let request, requestId;
  111. // It's time to send analytics. Clean up all requests and find the
  112. for (requestId in this.requests) {
  113. if (this.requests.hasOwnProperty(requestId)) {
  114. request = this.requests[requestId];
  115. if (request.timeSent < now - this.e2eping.analyticsIntervalMs) {
  116. // An old request. We don't care about it anymore.
  117. delete this.requests[requestId];
  118. } else if (request.rtt) {
  119. rtt = Math.min(rtt, request.rtt);
  120. }
  121. }
  122. }
  123. if (rtt < Infinity) {
  124. this.sendAnalytics(rtt);
  125. }
  126. }
  127. /**
  128. * Sends an analytics event for this participant with the given RTT.
  129. * @type {*}
  130. */
  131. sendAnalytics(rtt) {
  132. Statistics.sendAnalytics(createE2eRttEvent(
  133. this.id,
  134. this.participant.getProperty('region'),
  135. rtt));
  136. }
  137. }
  138. /**
  139. * Implements end-to-end ping (from one conference participant to another) via
  140. * the jitsi-videobridge channel (either WebRTC data channel or web socket).
  141. *
  142. * TODO: use a broadcast message instead of individual pings to each remote
  143. * participant.
  144. *
  145. * This class:
  146. * 1. Sends periodic ping requests to all other participants in the
  147. * conference.
  148. * 2. Responds to ping requests from other participants.
  149. * 3. Fires events with the end-to-end RTT to each participant whenever a
  150. * response is received.
  151. * 4. Fires analytics events with the end-to-end RTT periodically.
  152. */
  153. export default class E2ePing {
  154. /**
  155. * @param {JitsiConference} conference - The conference.
  156. * @param {Function} sendMessage - The function to use to send a message.
  157. * @param {Object} options
  158. */
  159. constructor(conference, options, sendMessage) {
  160. this.conference = conference;
  161. this.eventEmitter = conference.eventEmitter;
  162. this.sendMessage = sendMessage;
  163. // The interval at which pings will be sent (<= 0 disables sending).
  164. this.pingIntervalMs = 10000;
  165. // The interval at which analytics events will be sent.
  166. this.analyticsIntervalMs = 60000;
  167. // Maps a participant ID to its ParticipantWrapper
  168. this.participants = {};
  169. // Whether the WebRTC channel has been opened or not.
  170. this.isDataChannelOpen = false;
  171. if (options && options.e2eping) {
  172. if (typeof options.e2eping.pingInterval === 'number') {
  173. this.pingIntervalMs = options.e2eping.pingInterval;
  174. }
  175. if (typeof options.e2eping.analyticsInterval === 'number') {
  176. this.analyticsIntervalMs = options.e2eping.analyticsInterval;
  177. }
  178. // We want to report at most once a ping interval.
  179. if (this.analyticsIntervalMs > 0 && this.analyticsIntervalMs
  180. < this.pingIntervalMs) {
  181. this.analyticsIntervalMs = this.pingIntervalMs;
  182. }
  183. }
  184. logger.info(
  185. `Initializing e2e ping; pingInterval=${
  186. this.pingIntervalMs}, analyticsInterval=${
  187. this.analyticsIntervalMs}.`);
  188. this.participantJoined = this.participantJoined.bind(this);
  189. conference.on(
  190. JitsiConferenceEvents.USER_JOINED,
  191. this.participantJoined);
  192. this.participantLeft = this.participantLeft.bind(this);
  193. conference.on(
  194. JitsiConferenceEvents.USER_LEFT,
  195. this.participantLeft);
  196. this.messageReceived = this.messageReceived.bind(this);
  197. conference.on(
  198. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  199. this.messageReceived);
  200. this.dataChannelOpened = this.dataChannelOpened.bind(this);
  201. conference.on(
  202. JitsiConferenceEvents.DATA_CHANNEL_OPENED,
  203. this.dataChannelOpened);
  204. }
  205. /**
  206. * Notifies this instance that the communications channel has been opened
  207. * and it can now send messages via sendMessage.
  208. */
  209. dataChannelOpened() {
  210. this.isDataChannelOpen = true;
  211. // We don't want to wait the whole interval before sending the first
  212. // request, but we can't send it immediately after the participant joins
  213. // either, because our data channel might not have initialized.
  214. // So once the data channel initializes, send requests to everyone.
  215. // Wait an additional 200ms to give a chance to the remote side (if it
  216. // also just connected as is the case for the first 2 participants in a
  217. // conference) to open its data channel.
  218. for (const id in this.participants) {
  219. if (this.participants.hasOwnProperty(id)) {
  220. const participantWrapper = this.participants[id];
  221. window.setTimeout(participantWrapper.sendRequest, 200);
  222. }
  223. }
  224. }
  225. /**
  226. * Handles a message that was received.
  227. *
  228. * @param participant - The message sender.
  229. * @param payload - The payload of the message.
  230. */
  231. messageReceived(participant, payload) {
  232. // Listen to E2E PING requests and responses from other participants
  233. // in the conference.
  234. if (payload.type === E2E_PING_REQUEST) {
  235. this.handleRequest(participant.getId(), payload);
  236. } else if (payload.type === E2E_PING_RESPONSE) {
  237. this.handleResponse(participant.getId(), payload);
  238. }
  239. }
  240. /**
  241. * Handles a participant joining the conference. Starts to send ping
  242. * requests to the participant.
  243. *
  244. * @param {String} id - The ID of the participant.
  245. * @param {JitsiParticipant} participant - The participant that joined.
  246. */
  247. participantJoined(id, participant) {
  248. if (this.pingIntervalMs <= 0) {
  249. return;
  250. }
  251. if (this.participants[id]) {
  252. logger.info(
  253. `Participant wrapper already exists for ${id}. Clearing.`);
  254. this.participants[id].clearIntervals();
  255. delete this.participants[id];
  256. }
  257. this.participants[id] = new ParticipantWrapper(participant, this);
  258. }
  259. /**
  260. * Handles a participant leaving the conference. Stops sending requests.
  261. *
  262. * @param {String} id - The ID of the participant.
  263. */
  264. participantLeft(id) {
  265. if (this.pingIntervalMs <= 0) {
  266. return;
  267. }
  268. if (this.participants[id]) {
  269. this.participants[id].clearIntervals();
  270. delete this.participants[id];
  271. }
  272. }
  273. /**
  274. * Handles a ping request coming from another participant.
  275. *
  276. * @param {string} participantId - The ID of the participant who sent the
  277. * request.
  278. * @param {Object} request - The request.
  279. */
  280. handleRequest(participantId, request) {
  281. // If it's a valid request, just send a response.
  282. if (request && request.id) {
  283. const response = {
  284. type: E2E_PING_RESPONSE,
  285. id: request.id
  286. };
  287. this.sendMessage(response, participantId);
  288. } else {
  289. logger.info(
  290. `Received an invalid e2e ping request from ${participantId}.`);
  291. }
  292. }
  293. /**
  294. * Handles a ping response coming from another participant
  295. * @param {string} participantId - The ID of the participant who sent the
  296. * response.
  297. * @param {Object} response - The response.
  298. */
  299. handleResponse(participantId, response) {
  300. const participantWrapper = this.participants[participantId];
  301. if (participantWrapper) {
  302. participantWrapper.handleResponse(response);
  303. }
  304. }
  305. /**
  306. * Stops this E2ePing (i.e. stop sending requests).
  307. */
  308. stop() {
  309. logger.info('Stopping e2eping');
  310. this.conference.off(
  311. JitsiConferenceEvents.USER_JOINED,
  312. this.participantJoined);
  313. this.conference.off(
  314. JitsiConferenceEvents.USER_LEFT,
  315. this.participantLeft);
  316. this.conference.off(
  317. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  318. this.messageReceived);
  319. this.conference.off(
  320. JitsiConferenceEvents.DATA_CHANNEL_OPENED,
  321. this.dataChannelOpened);
  322. for (const id in this.participants) {
  323. if (this.participants.hasOwnProperty(id)) {
  324. this.participants[id].clearIntervals();
  325. }
  326. }
  327. this.participants = {};
  328. }
  329. }