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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import { getLogger } from '@jitsi/logger';
  2. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  3. const logger = getLogger(__filename);
  4. /**
  5. * The 'type' of a message which designates an e2e ping request.
  6. * @type {string}
  7. */
  8. const E2E_PING_REQUEST = 'e2e-ping-request';
  9. /**
  10. * The 'type' of a message which designates an e2e ping response.
  11. * @type {string}
  12. */
  13. const E2E_PING_RESPONSE = 'e2e-ping-response';
  14. /**
  15. * The number of requests to wait for before emitting an RTT value.
  16. */
  17. const DEFAULT_NUM_REQUESTS = 5;
  18. /**
  19. * The maximum number of messages per second to aim for. This is for the entire
  20. * conference, with the assumption that all endpoints join at once.
  21. */
  22. const DEFAULT_MAX_MESSAGES_PER_SECOND = 250;
  23. /**
  24. * The conference size beyond which e2e pings will be disabled.
  25. */
  26. const DEFAULT_MAX_CONFERENCE_SIZE = 200;
  27. /**
  28. * Saves e2e ping related state for a single JitsiParticipant.
  29. */
  30. class ParticipantWrapper {
  31. /**
  32. * Creates a ParticipantWrapper
  33. * @param {JitsiParticipant} participant - The remote participant that this
  34. * object wraps.
  35. * @param {E2ePing} e2eping
  36. */
  37. constructor(participant, e2eping) {
  38. // The JitsiParticipant
  39. this.participant = participant;
  40. // The E2ePing
  41. this.e2eping = e2eping;
  42. // Caches the ID
  43. this.id = participant.getId();
  44. // Recently sent requests
  45. this.requests = {};
  46. // The ID of the last sent request. We just increment it for each new
  47. // request. Start at 1 so we can consider only thruthy values valid.
  48. this.lastRequestId = 1;
  49. this.sendRequest = this.sendRequest.bind(this);
  50. this.handleResponse = this.handleResponse.bind(this);
  51. this.maybeLogRttAndStop = this.maybeLogRttAndStop.bind(this);
  52. this.scheduleNext = this.scheduleNext.bind(this);
  53. this.stop = this.stop.bind(this);
  54. this.getDelay = this.getDelay.bind(this);
  55. this.timeout = this.scheduleNext();
  56. }
  57. /**
  58. * Schedule the next ping to be sent.
  59. */
  60. scheduleNext() {
  61. return window.setTimeout(this.sendRequest, this.getDelay());
  62. }
  63. /**
  64. * Stop pinging this participant, canceling a scheduled ping, if any.
  65. */
  66. stop() {
  67. if (this.timeout) {
  68. window.clearTimeout(this.timeout);
  69. }
  70. this.e2eping.removeParticipant(this.id);
  71. }
  72. /**
  73. * Get the delay until the next ping in milliseconds.
  74. */
  75. getDelay() {
  76. const conferenceSize = this.e2eping.conference.getParticipants().length;
  77. const endpointPairs = conferenceSize * (conferenceSize - 1) / 2;
  78. const totalMessages = endpointPairs * this.e2eping.numRequests;
  79. const totalSeconds = totalMessages / this.e2eping.maxMessagesPerSecond;
  80. // Randomize between .5 and 1.5
  81. const r = 1.5 - Math.random();
  82. const delayBetweenMessages = r * Math.max(1000 * (totalSeconds / this.e2eping.numRequests), 1000);
  83. return delayBetweenMessages;
  84. }
  85. /**
  86. * Sends the next ping request.
  87. * @type {*}
  88. */
  89. sendRequest() {
  90. const requestId = this.lastRequestId++;
  91. const requestMessage = {
  92. type: E2E_PING_REQUEST,
  93. id: requestId
  94. };
  95. this.e2eping.sendMessage(requestMessage, this.id);
  96. this.requests[requestId] = {
  97. id: requestId,
  98. timeSent: window.performance.now()
  99. };
  100. }
  101. /**
  102. * Handles a response from this participant.
  103. * @type {*}
  104. */
  105. handleResponse(response) {
  106. const request = this.requests[response.id];
  107. if (request) {
  108. request.rtt = window.performance.now() - request.timeSent;
  109. }
  110. this.maybeLogRttAndStop();
  111. }
  112. /**
  113. * Check if we've received the pre-configured number of responses, and if
  114. * so log the measured RTT and stop sending requests.
  115. * @type {*}
  116. */
  117. maybeLogRttAndStop() {
  118. // The RTT we'll report is the minimum RTT measured
  119. let rtt = Infinity;
  120. let request, requestId;
  121. let numRequestsWithResponses = 0;
  122. let totalNumRequests = 0;
  123. for (requestId in this.requests) {
  124. if (this.requests.hasOwnProperty(requestId)) {
  125. request = this.requests[requestId];
  126. totalNumRequests++;
  127. if (request.rtt) {
  128. numRequestsWithResponses++;
  129. rtt = Math.min(rtt, request.rtt);
  130. }
  131. }
  132. }
  133. if (numRequestsWithResponses >= this.e2eping.numRequests) {
  134. logger.info(`Measured RTT=${rtt} ms to ${this.id} (in ${this.participant.getProperty('region')})`);
  135. this.stop();
  136. return;
  137. } else if (totalNumRequests > 2 * this.e2eping.numRequests) {
  138. logger.info(`Stopping e2eping for ${this.id} because we sent ${totalNumRequests} with only `
  139. + `${numRequestsWithResponses} responses.`);
  140. this.stop();
  141. return;
  142. }
  143. this.timeout = this.scheduleNext();
  144. }
  145. }
  146. /**
  147. * Implements end-to-end ping (from one conference participant to another) via
  148. * the jitsi-videobridge channel (either WebRTC data channel or web socket).
  149. *
  150. * TODO: use a broadcast message instead of individual pings to each remote
  151. * participant.
  152. *
  153. * This class:
  154. * 1. Sends periodic ping requests to all other participants in the
  155. * conference.
  156. * 2. Responds to ping requests from other participants.
  157. * 3. Fires events with the end-to-end RTT to each participant whenever a
  158. * response is received.
  159. * 4. Fires analytics events with the end-to-end RTT periodically.
  160. */
  161. export default class E2ePing {
  162. /**
  163. * @param {JitsiConference} conference - The conference.
  164. * @param {Function} sendMessage - The function to use to send a message.
  165. * @param {Object} options
  166. */
  167. constructor(conference, options, sendMessage) {
  168. this.conference = conference;
  169. this.eventEmitter = conference.eventEmitter;
  170. this.sendMessage = sendMessage;
  171. // Maps a participant ID to its ParticipantWrapper
  172. this.participants = {};
  173. this.numRequests = DEFAULT_NUM_REQUESTS;
  174. this.maxConferenceSize = DEFAULT_MAX_CONFERENCE_SIZE;
  175. this.maxMessagesPerSecond = DEFAULT_MAX_MESSAGES_PER_SECOND;
  176. if (options && options.e2eping) {
  177. if (typeof options.e2eping.numRequests === 'number') {
  178. this.numRequests = options.e2eping.numRequests;
  179. }
  180. if (typeof options.e2eping.maxConferenceSize === 'number') {
  181. this.maxConferenceSize = options.e2eping.maxConferenceSize;
  182. }
  183. if (typeof options.e2eping.maxMessagesPerSecond === 'number') {
  184. this.maxMessagesPerSecond = options.e2eping.maxMessagesPerSecond;
  185. }
  186. }
  187. logger.info(
  188. `Initializing e2e ping with numRequests=${this.numRequests}, maxConferenceSize=${this.maxConferenceSize}, `
  189. + `maxMessagesPerSecond=${this.maxMessagesPerSecond}.`);
  190. this.participantJoined = this.participantJoined.bind(this);
  191. this.participantLeft = this.participantLeft.bind(this);
  192. conference.on(JitsiConferenceEvents.USER_LEFT, this.participantLeft);
  193. this.messageReceived = this.messageReceived.bind(this);
  194. conference.on(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, this.messageReceived);
  195. this.conferenceJoined = this.conferenceJoined.bind(this);
  196. conference.on(JitsiConferenceEvents.CONFERENCE_JOINED, this.conferenceJoined);
  197. }
  198. /**
  199. * Delay processing USER_JOINED events until the MUC is fully joined,
  200. * otherwise the apparent conference size will be wrong.
  201. */
  202. conferenceJoined() {
  203. this.conference.getParticipants().forEach(p => this.participantJoined(p.getId(), p));
  204. this.conference.on(JitsiConferenceEvents.USER_JOINED, this.participantJoined);
  205. }
  206. /**
  207. * Handles a message that was received.
  208. *
  209. * @param participant - The message sender.
  210. * @param payload - The payload of the message.
  211. */
  212. messageReceived(participant, payload) {
  213. // Listen to E2E PING requests and responses from other participants
  214. // in the conference.
  215. if (payload.type === E2E_PING_REQUEST) {
  216. this.handleRequest(participant.getId(), payload);
  217. } else if (payload.type === E2E_PING_RESPONSE) {
  218. this.handleResponse(participant.getId(), payload);
  219. }
  220. }
  221. /**
  222. * Handles a participant joining the conference. Starts to send ping
  223. * requests to the participant.
  224. *
  225. * @param {String} id - The ID of the participant.
  226. * @param {JitsiParticipant} participant - The participant that joined.
  227. */
  228. participantJoined(id, participant) {
  229. if (this.participants[id]) {
  230. logger.info(`Participant wrapper already exists for ${id}. Clearing.`);
  231. this.participants[id].stop();
  232. }
  233. if (this.conference.getParticipants().length > this.maxConferenceSize) {
  234. return;
  235. }
  236. // We don't need to send e2eping in both directions for a pair of
  237. // endpoints. Force only one direction with just string comparison of
  238. // the IDs.
  239. if (this.conference.myUserId() > id) {
  240. logger.info(`Starting e2eping for participant ${id}`);
  241. this.participants[id] = new ParticipantWrapper(participant, this);
  242. }
  243. }
  244. /**
  245. * Remove a participant without calling "stop".
  246. */
  247. removeParticipant(id) {
  248. if (this.participants[id]) {
  249. delete this.participants[id];
  250. }
  251. }
  252. /**
  253. * Handles a participant leaving the conference. Stops sending requests.
  254. *
  255. * @param {String} id - The ID of the participant.
  256. */
  257. participantLeft(id) {
  258. if (this.participants[id]) {
  259. this.participants[id].stop();
  260. delete this.participants[id];
  261. }
  262. }
  263. /**
  264. * Handles a ping request coming from another participant.
  265. *
  266. * @param {string} participantId - The ID of the participant who sent the
  267. * request.
  268. * @param {Object} request - The request.
  269. */
  270. handleRequest(participantId, request) {
  271. // If it's a valid request, just send a response.
  272. if (request && request.id) {
  273. const response = {
  274. type: E2E_PING_RESPONSE,
  275. id: request.id
  276. };
  277. this.sendMessage(response, participantId);
  278. } else {
  279. logger.info(`Received an invalid e2e ping request from ${participantId}.`);
  280. }
  281. }
  282. /**
  283. * Handles a ping response coming from another participant
  284. * @param {string} participantId - The ID of the participant who sent the
  285. * response.
  286. * @param {Object} response - The response.
  287. */
  288. handleResponse(participantId, response) {
  289. const participantWrapper = this.participants[participantId];
  290. if (participantWrapper) {
  291. participantWrapper.handleResponse(response);
  292. }
  293. }
  294. /**
  295. * Stops this E2ePing (i.e. stop sending requests).
  296. */
  297. stop() {
  298. logger.info('Stopping e2eping');
  299. this.conference.off(JitsiConferenceEvents.USER_JOINED, this.participantJoined);
  300. this.conference.off(JitsiConferenceEvents.USER_LEFT, this.participantLeft);
  301. this.conference.off(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, this.messageReceived);
  302. for (const id in this.participants) {
  303. if (this.participants.hasOwnProperty(id)) {
  304. this.participants[id].stop();
  305. }
  306. }
  307. this.participants = {};
  308. }
  309. }