Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ParticipantConnectionStatus.js 28KB

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