Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ParticipantConnectionStatus.js 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /* global __filename */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  4. import * as JitsiTrackEvents from '../../JitsiTrackEvents';
  5. import * as MediaType from '../../service/RTC/MediaType';
  6. import browser from '../browser';
  7. import RTCEvents from '../../service/RTC/RTCEvents';
  8. import Statistics from '../statistics/statistics';
  9. import { createParticipantConnectionStatusEvent } from '../../service/statistics/AnalyticsEvents';
  10. const logger = getLogger(__filename);
  11. /**
  12. * Default value of 500 milliseconds for
  13. * {@link ParticipantConnectionStatus.outOfLastNTimeout}.
  14. *
  15. * @type {number}
  16. */
  17. const DEFAULT_NOT_IN_LAST_N_TIMEOUT = 500;
  18. /**
  19. * Default value of 2000 milliseconds for
  20. * {@link ParticipantConnectionStatus.rtcMuteTimeout}.
  21. *
  22. * @type {number}
  23. */
  24. const DEFAULT_RTC_MUTE_TIMEOUT = 10000;
  25. /**
  26. * The time to wait a track to be restored. Track which was out of lastN
  27. * should be inactive and when entering lastN it becomes restoring and when
  28. * data is received from bridge it will become active, but if no data is
  29. * received for some time we set status of that participant connection to
  30. * interrupted.
  31. * @type {number}
  32. */
  33. const DEFAULT_RESTORING_TIMEOUT = 10000;
  34. /**
  35. * Participant connection statuses.
  36. *
  37. * @type {{
  38. * ACTIVE: string,
  39. * INACTIVE: string,
  40. * INTERRUPTED: string,
  41. * RESTORING: string
  42. * }}
  43. */
  44. export const ParticipantConnectionStatus = {
  45. /**
  46. * Status indicating that connection is currently active.
  47. */
  48. ACTIVE: 'active',
  49. /**
  50. * Status indicating that connection is currently inactive.
  51. * Inactive means the connection was stopped on purpose from the bridge,
  52. * like exiting lastN or adaptivity decided to drop video because of not
  53. * enough bandwidth.
  54. */
  55. INACTIVE: 'inactive',
  56. /**
  57. * Status indicating that connection is currently interrupted.
  58. */
  59. INTERRUPTED: 'interrupted',
  60. /**
  61. * Status indicating that connection is currently restoring.
  62. */
  63. RESTORING: 'restoring'
  64. };
  65. /**
  66. * Class is responsible for emitting
  67. * JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED events.
  68. */
  69. export default class ParticipantConnectionStatusHandler {
  70. /* eslint-disable max-params*/
  71. /**
  72. * Calculates the new {@link ParticipantConnectionStatus} based on
  73. * the values given for some specific remote user. It is assumed that
  74. * the conference is currently in the JVB mode (in contrary to the P2P mode)
  75. * @param {boolean} isConnectionActiveByJvb true if the JVB did not get any
  76. * data from the user for the last 15 seconds.
  77. * @param {boolean} isInLastN indicates whether the user is in the last N
  78. * set. When set to false it means that JVB is not sending any video for
  79. * the user.
  80. * @param {boolean} isRestoringTimedout if true it means that the user has
  81. * been outside of last N too long to be considered
  82. * {@link ParticipantConnectionStatus.RESTORING}.
  83. * @param {boolean} isVideoMuted true if the user is video muted and we
  84. * should not expect to receive any video.
  85. * @param {boolean} isVideoTrackFrozen if the current browser support video
  86. * frozen detection then it will be set to true when the video track is
  87. * frozen. If the current browser does not support frozen detection the it's
  88. * always false.
  89. * @return {ParticipantConnectionStatus} the new connection status for
  90. * the user for whom the values above were provided.
  91. * @private
  92. */
  93. static _getNewStateForJvbMode(
  94. isConnectionActiveByJvb,
  95. isInLastN,
  96. isRestoringTimedout,
  97. isVideoMuted,
  98. isVideoTrackFrozen) {
  99. if (!isConnectionActiveByJvb) {
  100. // when there is a connection problem signaled from jvb
  101. // it means no media was flowing for at least 15secs, so both audio
  102. // and video are most likely interrupted
  103. return ParticipantConnectionStatus.INTERRUPTED;
  104. } else if (isVideoMuted) {
  105. // If the connection is active according to JVB and the user is
  106. // video muted there is no way for the connection to be inactive,
  107. // because the detection logic below only makes sense for video.
  108. return ParticipantConnectionStatus.ACTIVE;
  109. }
  110. // Logic when isVideoTrackFrozen is supported
  111. if (browser.supportsVideoMuteOnConnInterrupted()) {
  112. if (!isVideoTrackFrozen) {
  113. // If the video is playing we're good
  114. return ParticipantConnectionStatus.ACTIVE;
  115. } else if (isInLastN) {
  116. return isRestoringTimedout
  117. ? ParticipantConnectionStatus.INTERRUPTED
  118. : ParticipantConnectionStatus.RESTORING;
  119. }
  120. return ParticipantConnectionStatus.INACTIVE;
  121. }
  122. // Because this browser is incapable of detecting frozen video we must
  123. // rely on the lastN value
  124. return isInLastN
  125. ? ParticipantConnectionStatus.ACTIVE
  126. : ParticipantConnectionStatus.INACTIVE;
  127. }
  128. /* eslint-enable max-params*/
  129. /**
  130. * In P2P mode we don't care about any values coming from the JVB and
  131. * the connection status can be only active or interrupted.
  132. * @param {boolean} isVideoMuted the user if video muted
  133. * @param {boolean} isVideoTrackFrozen true if the video track for
  134. * the remote user is currently frozen. If the current browser does not
  135. * support video frozen detection then it's always false.
  136. * @return {ParticipantConnectionStatus}
  137. * @private
  138. */
  139. static _getNewStateForP2PMode(isVideoMuted, isVideoTrackFrozen) {
  140. if (!browser.supportsVideoMuteOnConnInterrupted()) {
  141. // There's no way to detect problems in P2P when there's no video
  142. // track frozen detection...
  143. return ParticipantConnectionStatus.ACTIVE;
  144. }
  145. return isVideoMuted || !isVideoTrackFrozen
  146. ? ParticipantConnectionStatus.ACTIVE
  147. : ParticipantConnectionStatus.INTERRUPTED;
  148. }
  149. /**
  150. * Creates new instance of <tt>ParticipantConnectionStatus</tt>.
  151. *
  152. * @constructor
  153. * @param {RTC} rtc the RTC service instance
  154. * @param {JitsiConference} conference parent conference instance
  155. * @param {Object} options
  156. * @param {number} [options.rtcMuteTimeout=2000] custom value for
  157. * {@link ParticipantConnectionStatus.rtcMuteTimeout}.
  158. * @param {number} [options.outOfLastNTimeout=500] custom value for
  159. * {@link ParticipantConnectionStatus.outOfLastNTimeout}.
  160. */
  161. constructor(rtc, conference, options) {
  162. this.rtc = rtc;
  163. this.conference = conference;
  164. /**
  165. * A map of the "endpoint ID"(which corresponds to the resource part
  166. * of MUC JID(nickname)) to the timeout callback IDs scheduled using
  167. * window.setTimeout.
  168. * @type {Object.<string, number>}
  169. */
  170. this.trackTimers = {};
  171. /**
  172. * This map holds the endpoint connection status received from the JVB
  173. * (as it might be different than the one stored in JitsiParticipant).
  174. * Required for getting back in sync when remote video track is removed.
  175. * @type {Object.<string, boolean>}
  176. */
  177. this.connStatusFromJvb = { };
  178. /**
  179. * If video track frozen detection through RTC mute event is supported,
  180. * we wait some time until video track is considered frozen. But because
  181. * when the user falls out of last N it is expected for the video to
  182. * freeze this timeout must be significantly reduced in "out of last N"
  183. * case.
  184. *
  185. * Basically this value is used instead of {@link rtcMuteTimeout} when
  186. * user is not in last N.
  187. * @type {number}
  188. */
  189. this.outOfLastNTimeout
  190. = typeof options.outOfLastNTimeout === 'number'
  191. ? options.outOfLastNTimeout : DEFAULT_NOT_IN_LAST_N_TIMEOUT;
  192. /**
  193. * How long we're going to wait after the RTC video track muted event
  194. * for the corresponding signalling mute event, before the connection
  195. * interrupted is fired. The default value is
  196. * {@link DEFAULT_RTC_MUTE_TIMEOUT}.
  197. *
  198. * @type {number} amount of time in milliseconds
  199. */
  200. this.rtcMuteTimeout
  201. = typeof options.rtcMuteTimeout === 'number'
  202. ? options.rtcMuteTimeout : DEFAULT_RTC_MUTE_TIMEOUT;
  203. /**
  204. * This map holds a timestamp indicating when participant's video track
  205. * was RTC muted (it is assumed that each participant can have only 1
  206. * video track at a time). The purpose of storing the timestamp is to
  207. * avoid the transition to disconnected status in case of legitimate
  208. * video mute operation where the signalling video muted event can
  209. * arrive shortly after RTC muted event.
  210. *
  211. * The key is participant's ID which is the same as endpoint id in
  212. * the Colibri conference allocated on the JVB.
  213. *
  214. * The value is a timestamp measured in milliseconds obtained with
  215. * <tt>Date.now()</tt>.
  216. *
  217. * FIXME merge this logic with NO_DATA_FROM_SOURCE event
  218. * implemented in JitsiLocalTrack by extending the event to
  219. * the remote track and allowing to set different timeout for
  220. * local and remote tracks.
  221. *
  222. * @type {Object.<string, number>}
  223. */
  224. this.rtcMutedTimestamp = { };
  225. logger.info(`RtcMuteTimeout set to: ${this.rtcMuteTimeout}`);
  226. /**
  227. * This map holds the timestamps indicating when participant's video
  228. * entered lastN set. Participants entering lastN will have connection
  229. * status restoring and when we start receiving video will become
  230. * active, but if video is not received for certain time
  231. * {@link DEFAULT_RESTORING_TIMEOUT} that participant connection status
  232. * will become interrupted.
  233. *
  234. * @type {Map<string, number>}
  235. */
  236. this.enteredLastNTimestamp = new Map();
  237. /**
  238. * A map of the "endpoint ID"(which corresponds to the resource part
  239. * of MUC JID(nickname)) to the restoring timeout callback IDs
  240. * scheduled using window.setTimeout.
  241. *
  242. * @type {Map<string, number>}
  243. */
  244. this.restoringTimers = new Map();
  245. /**
  246. * A map that holds the current connection status (along with all the internal events that happen
  247. * while in that state).
  248. *
  249. * The goal is to send this information to the analytics backend for post-mortem analysis.
  250. */
  251. this.peerConnStatusMap = new Map();
  252. }
  253. /**
  254. * Gets the video frozen timeout for given user.
  255. * @param {string} id endpoint/participant ID
  256. * @return {number} how long are we going to wait since RTC video muted
  257. * even, before a video track is considered frozen.
  258. * @private
  259. */
  260. _getVideoFrozenTimeout(id) {
  261. return this.rtc.isInLastN(id)
  262. ? this.rtcMuteTimeout : this.outOfLastNTimeout;
  263. }
  264. /**
  265. * Initializes <tt>ParticipantConnectionStatus</tt> and bind required event
  266. * listeners.
  267. */
  268. init() {
  269. this._onEndpointConnStatusChanged
  270. = this.onEndpointConnStatusChanged.bind(this);
  271. this.rtc.addListener(
  272. RTCEvents.ENDPOINT_CONN_STATUS_CHANGED,
  273. this._onEndpointConnStatusChanged);
  274. // Handles P2P status changes
  275. this._onP2PStatus = this.refreshConnectionStatusForAll.bind(this);
  276. this.conference.on(JitsiConferenceEvents.P2P_STATUS, this._onP2PStatus);
  277. // On some browsers MediaStreamTrack trigger "onmute"/"onunmute"
  278. // events for video type tracks when they stop receiving data which is
  279. // often a sign that remote user is having connectivity issues
  280. if (browser.supportsVideoMuteOnConnInterrupted()) {
  281. this._onTrackRtcMuted = this.onTrackRtcMuted.bind(this);
  282. this.rtc.addListener(
  283. RTCEvents.REMOTE_TRACK_MUTE, this._onTrackRtcMuted);
  284. this._onTrackRtcUnmuted = this.onTrackRtcUnmuted.bind(this);
  285. this.rtc.addListener(
  286. RTCEvents.REMOTE_TRACK_UNMUTE, this._onTrackRtcUnmuted);
  287. // Track added/removed listeners are used to bind "mute"/"unmute"
  288. // event handlers
  289. this._onRemoteTrackAdded = this.onRemoteTrackAdded.bind(this);
  290. this.conference.on(
  291. JitsiConferenceEvents.TRACK_ADDED,
  292. this._onRemoteTrackAdded);
  293. this._onRemoteTrackRemoved = this.onRemoteTrackRemoved.bind(this);
  294. this.conference.on(
  295. JitsiConferenceEvents.TRACK_REMOVED,
  296. this._onRemoteTrackRemoved);
  297. // Listened which will be bound to JitsiRemoteTrack to listen for
  298. // signalling mute/unmute events.
  299. this._onSignallingMuteChanged
  300. = this.onSignallingMuteChanged.bind(this);
  301. }
  302. this._onLastNChanged = this._onLastNChanged.bind(this);
  303. this.conference.on(
  304. JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  305. this._onLastNChanged);
  306. this._onLastNValueChanged
  307. = this.refreshConnectionStatusForAll.bind(this);
  308. this.rtc.on(
  309. RTCEvents.LASTN_VALUE_CHANGED, this._onLastNValueChanged);
  310. }
  311. /**
  312. * Removes all event listeners and disposes of all resources held by this
  313. * instance.
  314. */
  315. dispose() {
  316. this.rtc.removeListener(
  317. RTCEvents.ENDPOINT_CONN_STATUS_CHANGED,
  318. this._onEndpointConnStatusChanged);
  319. if (browser.supportsVideoMuteOnConnInterrupted()) {
  320. this.rtc.removeListener(
  321. RTCEvents.REMOTE_TRACK_MUTE,
  322. this._onTrackRtcMuted);
  323. this.rtc.removeListener(
  324. RTCEvents.REMOTE_TRACK_UNMUTE,
  325. this._onTrackRtcUnmuted);
  326. this.conference.off(
  327. JitsiConferenceEvents.TRACK_ADDED,
  328. this._onRemoteTrackAdded);
  329. this.conference.off(
  330. JitsiConferenceEvents.TRACK_REMOVED,
  331. this._onRemoteTrackRemoved);
  332. }
  333. this.conference.off(
  334. JitsiConferenceEvents.LAST_N_ENDPOINTS_CHANGED,
  335. this._onLastNChanged);
  336. this.rtc.removeListener(
  337. RTCEvents.LASTN_VALUE_CHANGED, this._onLastNValueChanged);
  338. this.conference.off(
  339. JitsiConferenceEvents.P2P_STATUS, this._onP2PStatus);
  340. const participantIds = Object.keys(this.trackTimers);
  341. for (const participantId of participantIds) {
  342. this.clearTimeout(participantId);
  343. this.clearRtcMutedTimestamp(participantId);
  344. }
  345. // Clear RTC connection status cache
  346. this.connStatusFromJvb = {};
  347. }
  348. /**
  349. * Handles RTCEvents.ENDPOINT_CONN_STATUS_CHANGED triggered when we receive
  350. * notification over the data channel from the bridge about endpoint's
  351. * connection status update.
  352. * @param {string} endpointId the endpoint ID(MUC nickname/resource JID)
  353. * @param {boolean} isActive true if the connection is OK or false otherwise
  354. */
  355. onEndpointConnStatusChanged(endpointId, isActive) {
  356. logger.debug(
  357. `Detector RTCEvents.ENDPOINT_CONN_STATUS_CHANGED(${Date.now()}): ${
  358. endpointId}: ${isActive}`);
  359. // Filter out events for the local JID for now
  360. if (endpointId !== this.conference.myUserId()) {
  361. // Store the status received over the data channels
  362. this.connStatusFromJvb[endpointId] = isActive;
  363. this.figureOutConnectionStatus(endpointId);
  364. }
  365. }
  366. /**
  367. * Changes connection status.
  368. * @param {JitsiParticipant} participant
  369. * @param newStatus
  370. */
  371. _changeConnectionStatus(participant, newStatus) {
  372. if (participant.getConnectionStatus() !== newStatus) {
  373. const endpointId = participant.getId();
  374. participant._setConnectionStatus(newStatus);
  375. logger.debug(
  376. `Emit endpoint conn status(${Date.now()}) ${endpointId}: ${
  377. newStatus}`);
  378. // Log the event on CallStats
  379. Statistics.sendLog(
  380. JSON.stringify({
  381. id: 'peer.conn.status',
  382. participant: endpointId,
  383. status: newStatus
  384. }));
  385. this.conference.eventEmitter.emit(
  386. JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED,
  387. endpointId, newStatus);
  388. }
  389. }
  390. /**
  391. * Reset the postponed "connection interrupted" event which was previously
  392. * scheduled as a timeout on RTC 'onmute' event.
  393. *
  394. * @param {string} participantId the participant for which the "connection
  395. * interrupted" timeout was scheduled
  396. */
  397. clearTimeout(participantId) {
  398. if (this.trackTimers[participantId]) {
  399. window.clearTimeout(this.trackTimers[participantId]);
  400. this.trackTimers[participantId] = null;
  401. }
  402. }
  403. /**
  404. * Clears the timestamp of the RTC muted event for participant's video track
  405. * @param {string} participantId the id of the conference participant which
  406. * is the same as the Colibri endpoint ID of the video channel allocated for
  407. * the user on the videobridge.
  408. */
  409. clearRtcMutedTimestamp(participantId) {
  410. this.rtcMutedTimestamp[participantId] = null;
  411. }
  412. /**
  413. * Bind signalling mute event listeners for video {JitsiRemoteTrack} when
  414. * a new one is added to the conference.
  415. *
  416. * @param {JitsiTrack} remoteTrack the {JitsiTrack} which is being added to
  417. * the conference.
  418. */
  419. onRemoteTrackAdded(remoteTrack) {
  420. if (!remoteTrack.isLocal()
  421. && remoteTrack.getType() === MediaType.VIDEO) {
  422. logger.debug(
  423. `Detector on remote track added for: ${
  424. remoteTrack.getParticipantId()}`);
  425. remoteTrack.on(
  426. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  427. this._onSignallingMuteChanged);
  428. }
  429. }
  430. /**
  431. * Removes all event listeners bound to the remote video track and clears
  432. * any related timeouts.
  433. *
  434. * @param {JitsiRemoteTrack} remoteTrack the remote track which is being
  435. * removed from the conference.
  436. */
  437. onRemoteTrackRemoved(remoteTrack) {
  438. if (!remoteTrack.isLocal()
  439. && remoteTrack.getType() === MediaType.VIDEO) {
  440. const endpointId = remoteTrack.getParticipantId();
  441. logger.debug(`Detector on remote track removed: ${endpointId}`);
  442. remoteTrack.off(
  443. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  444. this._onSignallingMuteChanged);
  445. this.clearTimeout(endpointId);
  446. this.clearRtcMutedTimestamp(endpointId);
  447. this.figureOutConnectionStatus(endpointId);
  448. }
  449. }
  450. /**
  451. * Checks if given participant's video is considered frozen.
  452. * @param {JitsiParticipant} participant
  453. * @return {boolean} <tt>true</tt> if the video has frozen for given
  454. * participant or <tt>false</tt> when it's either not considered frozen
  455. * (yet) or if freeze detection is not supported by the current browser.
  456. *
  457. * FIXME merge this logic with NO_DATA_FROM_SOURCE event
  458. * implemented in JitsiLocalTrack by extending the event to
  459. * the remote track and allowing to set different timeout for
  460. * local and remote tracks.
  461. *
  462. */
  463. isVideoTrackFrozen(participant) {
  464. if (!browser.supportsVideoMuteOnConnInterrupted()) {
  465. return false;
  466. }
  467. const id = participant.getId();
  468. const hasAnyVideoRTCMuted = participant.hasAnyVideoTrackWebRTCMuted();
  469. const rtcMutedTimestamp = this.rtcMutedTimestamp[id];
  470. const timeout = this._getVideoFrozenTimeout(id);
  471. return hasAnyVideoRTCMuted
  472. && typeof rtcMutedTimestamp === 'number'
  473. && (Date.now() - rtcMutedTimestamp) >= timeout;
  474. }
  475. /**
  476. * Goes over every participant and updates connectivity status.
  477. * Should be called when a parameter which affects all of the participants
  478. * is changed (P2P for example).
  479. */
  480. refreshConnectionStatusForAll() {
  481. const participants = this.conference.getParticipants();
  482. for (const participant of participants) {
  483. this.figureOutConnectionStatus(participant.getId());
  484. }
  485. }
  486. /**
  487. * Figures out (and updates) the current connectivity status for
  488. * the participant identified by the given id.
  489. *
  490. * @param {string} id the participant's id (MUC nickname or Colibri endpoint
  491. * ID).
  492. */
  493. figureOutConnectionStatus(id) {
  494. const participant = this.conference.getParticipantById(id);
  495. if (!participant) {
  496. // Probably the participant is no longer in the conference
  497. // (at the time of writing this code, participant is
  498. // detached from the conference and TRACK_REMOVED events are
  499. // fired),
  500. // so we don't care, but let's print the warning for
  501. // debugging purpose
  502. logger.warn(`figure out conn status - no participant for: ${id}`);
  503. return;
  504. }
  505. const inP2PMode = this.conference.isP2PActive();
  506. const isRestoringTimedOut = this._isRestoringTimedout(id);
  507. const audioOnlyMode = this.rtc.getLastN() === 0;
  508. // NOTE Overriding videoMuted to true for audioOnlyMode should disable
  509. // any detection based on video playback or the last N.
  510. const isVideoMuted = participant.isVideoMuted() || audioOnlyMode;
  511. const isVideoTrackFrozen = this.isVideoTrackFrozen(participant);
  512. const isInLastN = this.rtc.isInLastN(id);
  513. let isConnActiveByJvb = this.connStatusFromJvb[id];
  514. if (typeof isConnActiveByJvb !== 'boolean') {
  515. // If no status was received from the JVB it means that it's active
  516. // (the bridge does not send notification unless there is a problem)
  517. logger.debug('Assuming connection active by JVB - no notification');
  518. isConnActiveByJvb = true;
  519. }
  520. const newState
  521. = inP2PMode
  522. ? ParticipantConnectionStatusHandler._getNewStateForP2PMode(
  523. isVideoMuted,
  524. isVideoTrackFrozen)
  525. : ParticipantConnectionStatusHandler._getNewStateForJvbMode(
  526. isConnActiveByJvb,
  527. isInLastN,
  528. isRestoringTimedOut,
  529. isVideoMuted,
  530. isVideoTrackFrozen);
  531. // if the new state is not restoring clear timers and timestamps
  532. // that we use to track the restoring state
  533. if (newState !== ParticipantConnectionStatus.RESTORING) {
  534. this._clearRestoringTimer(id);
  535. }
  536. logger.debug(
  537. `Figure out conn status for ${id}, is video muted: ${
  538. isVideoMuted} is active(jvb): ${
  539. isConnActiveByJvb} video track frozen: ${
  540. isVideoTrackFrozen} p2p mode: ${
  541. inP2PMode} is in last N: ${
  542. isInLastN} currentStatus => newStatus: ${
  543. participant.getConnectionStatus()} => ${newState}`);
  544. const nowMs = new Date().getTime();
  545. const internalState = {
  546. 'instantMs': nowMs,
  547. 'isVideoMuted': isVideoMuted,
  548. 'isConnActiveByJvb': isConnActiveByJvb,
  549. 'isVideoTrackFrozen': isVideoTrackFrozen,
  550. 'inP2PMode': inP2PMode,
  551. 'inInLastN': isInLastN
  552. };
  553. if (!this.peerConnStatusMap[id] || this.peerConnStatusMap[id].state !== newState) {
  554. // The peer connection status has changed. Compute the duration of the current
  555. // connection status and send it as an analytics event.
  556. if (this.peerConnStatusMap[id]) {
  557. this.peerConnStatusMap[id].value = nowMs - this.peerConnStatusMap[id].startedMs;
  558. Statistics.sendAnalytics(
  559. createParticipantConnectionStatusEvent(this.peerConnStatusMap[id]));
  560. }
  561. // And start a new status for the participant.
  562. this.peerConnStatusMap[id] = {
  563. 'internalStates': [ internalState ],
  564. 'state': newState,
  565. 'startedMs': nowMs
  566. };
  567. } else {
  568. // The connection status hasn't changed, but there was an internal state change.
  569. // Register the internal state.
  570. this.peerConnStatusMap[id].internalStates.push(internalState);
  571. }
  572. this._changeConnectionStatus(participant, newState);
  573. }
  574. /**
  575. * On change in Last N set check all leaving and entering participants to
  576. * change their corresponding statuses.
  577. *
  578. * @param {Array<string>} leavingLastN array of ids leaving lastN.
  579. * @param {Array<string>} enteringLastN array of ids entering lastN.
  580. * @private
  581. */
  582. _onLastNChanged(leavingLastN = [], enteringLastN = []) {
  583. const now = Date.now();
  584. logger.debug(
  585. 'leaving/entering lastN', leavingLastN, enteringLastN, now);
  586. for (const id of leavingLastN) {
  587. this.enteredLastNTimestamp.delete(id);
  588. this._clearRestoringTimer(id);
  589. this.figureOutConnectionStatus(id);
  590. }
  591. for (const id of enteringLastN) {
  592. // store the timestamp this id is entering lastN
  593. this.enteredLastNTimestamp.set(id, now);
  594. this.figureOutConnectionStatus(id);
  595. }
  596. }
  597. /**
  598. * Clears the restoring timer for participant's video track and the
  599. * timestamp for entering lastN.
  600. *
  601. * @param {string} participantId the id of the conference participant which
  602. * is the same as the Colibri endpoint ID of the video channel allocated for
  603. * the user on the videobridge.
  604. */
  605. _clearRestoringTimer(participantId) {
  606. const rTimer = this.restoringTimers.get(participantId);
  607. if (rTimer) {
  608. clearTimeout(rTimer);
  609. this.restoringTimers.delete(participantId);
  610. }
  611. }
  612. /**
  613. * Checks whether a track had stayed enough in restoring state, compares
  614. * current time and the time the track entered in lastN. If it hasn't
  615. * timedout and there is no timer added, add new timer in order to give it
  616. * more time to become active or mark it as interrupted on next check.
  617. *
  618. * @param {string} participantId the id of the conference participant which
  619. * is the same as the Colibri endpoint ID of the video channel allocated for
  620. * the user on the videobridge.
  621. * @returns {boolean} <tt>true</tt> if the track was in restoring state
  622. * more than the timeout ({@link DEFAULT_RESTORING_TIMEOUT}.) in order to
  623. * set its status to interrupted.
  624. * @private
  625. */
  626. _isRestoringTimedout(participantId) {
  627. const enteredLastNTimestamp
  628. = this.enteredLastNTimestamp.get(participantId);
  629. if (enteredLastNTimestamp
  630. && (Date.now() - enteredLastNTimestamp)
  631. >= DEFAULT_RESTORING_TIMEOUT) {
  632. return true;
  633. }
  634. // still haven't reached timeout, if there is no timer scheduled,
  635. // schedule one so we can track the restoring state and change it after
  636. // reaching the timeout
  637. const rTimer = this.restoringTimers.get(participantId);
  638. if (!rTimer) {
  639. this.restoringTimers.set(participantId, setTimeout(
  640. () => this.figureOutConnectionStatus(participantId),
  641. DEFAULT_RESTORING_TIMEOUT));
  642. }
  643. return false;
  644. }
  645. /**
  646. * Handles RTC 'onmute' event for the video track.
  647. *
  648. * @param {JitsiRemoteTrack} track the video track for which 'onmute' event
  649. * will be processed.
  650. */
  651. onTrackRtcMuted(track) {
  652. const participantId = track.getParticipantId();
  653. const participant = this.conference.getParticipantById(participantId);
  654. logger.debug(`Detector track RTC muted: ${participantId}`, Date.now());
  655. if (!participant) {
  656. logger.error(`No participant for id: ${participantId}`);
  657. return;
  658. }
  659. this.rtcMutedTimestamp[participantId] = Date.now();
  660. if (!participant.isVideoMuted()) {
  661. // If the user is not muted according to the signalling we'll give
  662. // it some time, before the connection interrupted event is
  663. // triggered.
  664. this.clearTimeout(participantId);
  665. // The timeout is reduced when user is not in the last N
  666. const timeout = this._getVideoFrozenTimeout(participantId);
  667. this.trackTimers[participantId] = window.setTimeout(() => {
  668. logger.debug(
  669. `Set RTC mute timeout for: ${participantId}\
  670. of ${timeout} ms`);
  671. this.clearTimeout(participantId);
  672. this.figureOutConnectionStatus(participantId);
  673. }, timeout);
  674. }
  675. }
  676. /**
  677. * Handles RTC 'onunmute' event for the video track.
  678. *
  679. * @param {JitsiRemoteTrack} track the video track for which 'onunmute'
  680. * event will be processed.
  681. */
  682. onTrackRtcUnmuted(track) {
  683. const participantId = track.getParticipantId();
  684. logger.debug(
  685. `Detector track RTC unmuted: ${participantId}`, Date.now());
  686. this.clearTimeout(participantId);
  687. this.clearRtcMutedTimestamp(participantId);
  688. this.figureOutConnectionStatus(participantId);
  689. }
  690. /**
  691. * Here the signalling "mute"/"unmute" events are processed.
  692. *
  693. * @param {JitsiRemoteTrack} track the remote video track for which
  694. * the signalling mute/unmute event will be processed.
  695. */
  696. onSignallingMuteChanged(track) {
  697. const participantId = track.getParticipantId();
  698. logger.debug(
  699. `Detector on track signalling mute changed: ${participantId}`,
  700. track.isMuted());
  701. this.figureOutConnectionStatus(participantId);
  702. }
  703. }