選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

e2eping.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. import { getLogger } from '@jitsi/logger';
  2. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  3. import * as E2ePingEvents
  4. from '../../service/e2eping/E2ePingEvents';
  5. import { createE2eRttEvent } from '../../service/statistics/AnalyticsEvents';
  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 {JitsiConference} conference - The conference.
  155. * @param {Function} sendMessage - The function to use to send a message.
  156. * @param {Object} options
  157. */
  158. constructor(conference, options, sendMessage) {
  159. this.conference = conference;
  160. this.eventEmitter = conference.eventEmitter;
  161. this.sendMessage = sendMessage;
  162. // The interval at which pings will be sent (<= 0 disables sending).
  163. this.pingIntervalMs = 10000;
  164. // The interval at which analytics events will be sent.
  165. this.analyticsIntervalMs = 60000;
  166. // Maps a participant ID to its ParticipantWrapper
  167. this.participants = {};
  168. // Whether the WebRTC channel has been opened or not.
  169. this.isDataChannelOpen = false;
  170. if (options && options.e2eping) {
  171. if (typeof options.e2eping.pingInterval === 'number') {
  172. this.pingIntervalMs = options.e2eping.pingInterval;
  173. }
  174. if (typeof options.e2eping.analyticsInterval === 'number') {
  175. this.analyticsIntervalMs = options.e2eping.analyticsInterval;
  176. }
  177. // We want to report at most once a ping interval.
  178. if (this.analyticsIntervalMs > 0 && this.analyticsIntervalMs
  179. < this.pingIntervalMs) {
  180. this.analyticsIntervalMs = this.pingIntervalMs;
  181. }
  182. }
  183. logger.info(
  184. `Initializing e2e ping; pingInterval=${
  185. this.pingIntervalMs}, analyticsInterval=${
  186. this.analyticsIntervalMs}.`);
  187. this.participantJoined = this.participantJoined.bind(this);
  188. conference.on(
  189. JitsiConferenceEvents.USER_JOINED,
  190. this.participantJoined);
  191. this.participantLeft = this.participantLeft.bind(this);
  192. conference.on(
  193. JitsiConferenceEvents.USER_LEFT,
  194. this.participantLeft);
  195. this.messageReceived = this.messageReceived.bind(this);
  196. conference.on(
  197. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  198. this.messageReceived);
  199. this.dataChannelOpened = this.dataChannelOpened.bind(this);
  200. conference.on(
  201. JitsiConferenceEvents.DATA_CHANNEL_OPENED,
  202. this.dataChannelOpened);
  203. }
  204. /**
  205. * Notifies this instance that the communications channel has been opened
  206. * and it can now send messages via sendMessage.
  207. */
  208. dataChannelOpened() {
  209. this.isDataChannelOpen = true;
  210. // We don't want to wait the whole interval before sending the first
  211. // request, but we can't send it immediately after the participant joins
  212. // either, because our data channel might not have initialized.
  213. // So once the data channel initializes, send requests to everyone.
  214. // Wait an additional 200ms to give a chance to the remote side (if it
  215. // also just connected as is the case for the first 2 participants in a
  216. // conference) to open its data channel.
  217. for (const id in this.participants) {
  218. if (this.participants.hasOwnProperty(id)) {
  219. const participantWrapper = this.participants[id];
  220. window.setTimeout(participantWrapper.sendRequest, 200);
  221. }
  222. }
  223. }
  224. /**
  225. * Handles a message that was received.
  226. *
  227. * @param participant - The message sender.
  228. * @param payload - The payload of the message.
  229. */
  230. messageReceived(participant, payload) {
  231. // Listen to E2E PING requests and responses from other participants
  232. // in the conference.
  233. if (payload.type === E2E_PING_REQUEST) {
  234. this.handleRequest(participant.getId(), payload);
  235. } else if (payload.type === E2E_PING_RESPONSE) {
  236. this.handleResponse(participant.getId(), payload);
  237. }
  238. }
  239. /**
  240. * Handles a participant joining the conference. Starts to send ping
  241. * requests to the participant.
  242. *
  243. * @param {String} id - The ID of the participant.
  244. * @param {JitsiParticipant} participant - The participant that joined.
  245. */
  246. participantJoined(id, participant) {
  247. if (this.pingIntervalMs <= 0) {
  248. return;
  249. }
  250. if (this.participants[id]) {
  251. logger.info(
  252. `Participant wrapper already exists for ${id}. Clearing.`);
  253. this.participants[id].clearIntervals();
  254. delete this.participants[id];
  255. }
  256. this.participants[id] = new ParticipantWrapper(participant, this);
  257. }
  258. /**
  259. * Handles a participant leaving the conference. Stops sending requests.
  260. *
  261. * @param {String} id - The ID of the participant.
  262. */
  263. participantLeft(id) {
  264. if (this.pingIntervalMs <= 0) {
  265. return;
  266. }
  267. if (this.participants[id]) {
  268. this.participants[id].clearIntervals();
  269. delete this.participants[id];
  270. }
  271. }
  272. /**
  273. * Handles a ping request coming from another participant.
  274. *
  275. * @param {string} participantId - The ID of the participant who sent the
  276. * request.
  277. * @param {Object} request - The request.
  278. */
  279. handleRequest(participantId, request) {
  280. // If it's a valid request, just send a response.
  281. if (request && request.id) {
  282. const response = {
  283. type: E2E_PING_RESPONSE,
  284. id: request.id
  285. };
  286. this.sendMessage(response, participantId);
  287. } else {
  288. logger.info(
  289. `Received an invalid e2e ping request from ${participantId}.`);
  290. }
  291. }
  292. /**
  293. * Handles a ping response coming from another participant
  294. * @param {string} participantId - The ID of the participant who sent the
  295. * response.
  296. * @param {Object} response - The response.
  297. */
  298. handleResponse(participantId, response) {
  299. const participantWrapper = this.participants[participantId];
  300. if (participantWrapper) {
  301. participantWrapper.handleResponse(response);
  302. }
  303. }
  304. /**
  305. * Stops this E2ePing (i.e. stop sending requests).
  306. */
  307. stop() {
  308. logger.info('Stopping e2eping');
  309. this.conference.off(
  310. JitsiConferenceEvents.USER_JOINED,
  311. this.participantJoined);
  312. this.conference.off(
  313. JitsiConferenceEvents.USER_LEFT,
  314. this.participantLeft);
  315. this.conference.off(
  316. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  317. this.messageReceived);
  318. this.conference.off(
  319. JitsiConferenceEvents.DATA_CHANNEL_OPENED,
  320. this.dataChannelOpened);
  321. for (const id in this.participants) {
  322. if (this.participants.hasOwnProperty(id)) {
  323. this.participants[id].clearIntervals();
  324. }
  325. }
  326. this.participants = {};
  327. }
  328. }