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 11KB

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