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.

ParticipantConnectionStatus.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 2000 milliseconds for
  12. * {@link ParticipantConnectionStatus.rtcMuteTimeout}.
  13. *
  14. * @type {number}
  15. */
  16. const DEFAULT_RTC_MUTE_TIMEOUT = 2000;
  17. /**
  18. * Class is responsible for emitting
  19. * JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED events.
  20. */
  21. export default class ParticipantConnectionStatus {
  22. /**
  23. * Creates new instance of <tt>ParticipantConnectionStatus</tt>.
  24. *
  25. * @constructor
  26. * @param {RTC} rtc the RTC service instance
  27. * @param {JitsiConference} conference parent conference instance
  28. * @param {number} rtcMuteTimeout (optional) custom value for
  29. * {@link ParticipantConnectionStatus.rtcMuteTimeout}.
  30. */
  31. constructor(rtc, conference, rtcMuteTimeout) {
  32. this.rtc = rtc;
  33. this.conference = conference;
  34. /**
  35. * A map of the "endpoint ID"(which corresponds to the resource part
  36. * of MUC JID(nickname)) to the timeout callback IDs scheduled using
  37. * window.setTimeout.
  38. * @type {Object.<string, number>}
  39. */
  40. this.trackTimers = {};
  41. /**
  42. * This map holds the endpoint connection status received from the JVB
  43. * (as it might be different than the one stored in JitsiParticipant).
  44. * Required for getting back in sync when remote video track is removed.
  45. * @type {Object.<string, boolean>}
  46. */
  47. this.connStatusFromJvb = { };
  48. /**
  49. * How long we're going to wait after the RTC video track muted event
  50. * for the corresponding signalling mute event, before the connection
  51. * interrupted is fired. The default value is
  52. * {@link DEFAULT_RTC_MUTE_TIMEOUT}.
  53. *
  54. * @type {number} amount of time in milliseconds
  55. */
  56. this.rtcMuteTimeout
  57. = typeof rtcMuteTimeout === 'number'
  58. ? rtcMuteTimeout : DEFAULT_RTC_MUTE_TIMEOUT;
  59. /**
  60. * This map holds a timestamp indicating when participant's video track
  61. * was RTC muted (it is assumed that each participant can have only 1
  62. * video track at a time). The purpose of storing the timestamp is to
  63. * avoid the transition to disconnected status in case of legitimate
  64. * video mute operation where the signalling video muted event can
  65. * arrive shortly after RTC muted event.
  66. *
  67. * The key is participant's ID which is the same as endpoint id in
  68. * the Colibri conference allocated on the JVB.
  69. *
  70. * The value is a timestamp measured in milliseconds obtained with
  71. * <tt>Date.now()</tt>.
  72. *
  73. * FIXME merge this logic with NO_DATA_FROM_SOURCE event
  74. * implemented in JitsiLocalTrack by extending the event to
  75. * the remote track and allowing to set different timeout for
  76. * local and remote tracks.
  77. *
  78. * @type {Object.<string, number>}
  79. */
  80. this.rtcMutedTimestamp = { };
  81. logger.info(`RtcMuteTimeout set to: ${this.rtcMuteTimeout}`);
  82. }
  83. /**
  84. * Initializes <tt>ParticipantConnectionStatus</tt> and bind required event
  85. * listeners.
  86. */
  87. init() {
  88. this._onEndpointConnStatusChanged
  89. = this.onEndpointConnStatusChanged.bind(this);
  90. this.rtc.addListener(
  91. RTCEvents.ENDPOINT_CONN_STATUS_CHANGED,
  92. this._onEndpointConnStatusChanged);
  93. // Handles P2P status changes
  94. this._onP2PStatus = this.refreshConnectionStatusForAll.bind(this);
  95. this.conference.on(JitsiConferenceEvents.P2P_STATUS, this._onP2PStatus);
  96. // On some browsers MediaStreamTrack trigger "onmute"/"onunmute"
  97. // events for video type tracks when they stop receiving data which is
  98. // often a sign that remote user is having connectivity issues
  99. if (RTCBrowserType.isVideoMuteOnConnInterruptedSupported()) {
  100. this._onTrackRtcMuted = this.onTrackRtcMuted.bind(this);
  101. this.rtc.addListener(
  102. RTCEvents.REMOTE_TRACK_MUTE, this._onTrackRtcMuted);
  103. this._onTrackRtcUnmuted = this.onTrackRtcUnmuted.bind(this);
  104. this.rtc.addListener(
  105. RTCEvents.REMOTE_TRACK_UNMUTE, this._onTrackRtcUnmuted);
  106. // Track added/removed listeners are used to bind "mute"/"unmute"
  107. // event handlers
  108. this._onRemoteTrackAdded = this.onRemoteTrackAdded.bind(this);
  109. this.conference.on(
  110. JitsiConferenceEvents.TRACK_ADDED,
  111. this._onRemoteTrackAdded);
  112. this._onRemoteTrackRemoved = this.onRemoteTrackRemoved.bind(this);
  113. this.conference.on(
  114. JitsiConferenceEvents.TRACK_REMOVED,
  115. this._onRemoteTrackRemoved);
  116. // Listened which will be bound to JitsiRemoteTrack to listen for
  117. // signalling mute/unmute events.
  118. this._onSignallingMuteChanged
  119. = this.onSignallingMuteChanged.bind(this);
  120. }
  121. }
  122. /**
  123. * Removes all event listeners and disposes of all resources held by this
  124. * instance.
  125. */
  126. dispose() {
  127. this.rtc.removeListener(
  128. RTCEvents.ENDPOINT_CONN_STATUS_CHANGED,
  129. this._onEndpointConnStatusChanged);
  130. if (RTCBrowserType.isVideoMuteOnConnInterruptedSupported()) {
  131. this.rtc.removeListener(
  132. RTCEvents.REMOTE_TRACK_MUTE,
  133. this._onTrackRtcMuted);
  134. this.rtc.removeListener(
  135. RTCEvents.REMOTE_TRACK_UNMUTE,
  136. this._onTrackRtcUnmuted);
  137. this.conference.off(
  138. JitsiConferenceEvents.TRACK_ADDED,
  139. this._onRemoteTrackAdded);
  140. this.conference.off(
  141. JitsiConferenceEvents.TRACK_REMOVED,
  142. this._onRemoteTrackRemoved);
  143. }
  144. this.conference.off(
  145. JitsiConferenceEvents.P2P_STATUS, this._onP2PStatus);
  146. const participantIds = Object.keys(this.trackTimers);
  147. for (const participantId of participantIds) {
  148. this.clearTimeout(participantId);
  149. this.clearRtcMutedTimestamp(participantId);
  150. }
  151. // Clear RTC connection status cache
  152. this.connStatusFromJvb = {};
  153. }
  154. /**
  155. * Handles RTCEvents.ENDPOINT_CONN_STATUS_CHANGED triggered when we receive
  156. * notification over the data channel from the bridge about endpoint's
  157. * connection status update.
  158. * @param endpointId {string} the endpoint ID(MUC nickname/resource JID)
  159. * @param isActive {boolean} true if the connection is OK or false otherwise
  160. */
  161. onEndpointConnStatusChanged(endpointId, isActive) {
  162. logger.debug(
  163. `Detector RTCEvents.ENDPOINT_CONN_STATUS_CHANGED(${Date.now()}): ${
  164. endpointId}: ${isActive}`);
  165. // Filter out events for the local JID for now
  166. if (endpointId !== this.conference.myUserId()) {
  167. // Store the status received over the data channels
  168. this.connStatusFromJvb[endpointId] = isActive;
  169. this.figureOutConnectionStatus(endpointId);
  170. }
  171. }
  172. /**
  173. *
  174. * @param participant
  175. * @param newStatus
  176. */
  177. _changeConnectionStatus(participant, newStatus) {
  178. if (participant.isConnectionActive() !== newStatus) {
  179. const endpointId = participant.getId();
  180. participant._setIsConnectionActive(newStatus);
  181. logger.debug(
  182. `Emit endpoint conn status(${Date.now()}) ${endpointId}: ${
  183. newStatus}`);
  184. // Log the event on CallStats
  185. Statistics.sendLog(
  186. JSON.stringify({
  187. id: 'peer.conn.status',
  188. participant: endpointId,
  189. status: newStatus
  190. }));
  191. // and analytics
  192. Statistics.analytics.sendEvent('peer.conn.status',
  193. { label: newStatus });
  194. this.conference.eventEmitter.emit(
  195. JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED,
  196. endpointId, newStatus);
  197. }
  198. }
  199. /**
  200. * Reset the postponed "connection interrupted" event which was previously
  201. * scheduled as a timeout on RTC 'onmute' event.
  202. *
  203. * @param participantId the participant for which the "connection
  204. * interrupted" timeout was scheduled
  205. */
  206. clearTimeout(participantId) {
  207. if (this.trackTimers[participantId]) {
  208. window.clearTimeout(this.trackTimers[participantId]);
  209. this.trackTimers[participantId] = null;
  210. }
  211. }
  212. /**
  213. * Clears the timestamp of the RTC muted event for participant's video track
  214. * @param participantId the id of the conference participant which is
  215. * the same as the Colibri endpoint ID of the video channel allocated for
  216. * the user on the videobridge.
  217. */
  218. clearRtcMutedTimestamp(participantId) {
  219. this.rtcMutedTimestamp[participantId] = null;
  220. }
  221. /**
  222. * Bind signalling mute event listeners for video {JitsiRemoteTrack} when
  223. * a new one is added to the conference.
  224. *
  225. * @param {JitsiTrack} remoteTrack the {JitsiTrack} which is being added to
  226. * the conference.
  227. */
  228. onRemoteTrackAdded(remoteTrack) {
  229. if (!remoteTrack.isLocal()
  230. && remoteTrack.getType() === MediaType.VIDEO) {
  231. logger.debug(
  232. `Detector on remote track added for: ${
  233. remoteTrack.getParticipantId()}`);
  234. remoteTrack.on(
  235. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  236. this._onSignallingMuteChanged);
  237. }
  238. }
  239. /**
  240. * Removes all event listeners bound to the remote video track and clears
  241. * any related timeouts.
  242. *
  243. * @param {JitsiRemoteTrack} remoteTrack the remote track which is being
  244. * removed from the conference.
  245. */
  246. onRemoteTrackRemoved(remoteTrack) {
  247. if (!remoteTrack.isLocal()
  248. && remoteTrack.getType() === MediaType.VIDEO) {
  249. const endpointId = remoteTrack.getParticipantId();
  250. logger.debug(`Detector on remote track removed: ${endpointId}`);
  251. remoteTrack.off(
  252. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  253. this._onSignallingMuteChanged);
  254. this.clearTimeout(endpointId);
  255. this.clearRtcMutedTimestamp(endpointId);
  256. this.figureOutConnectionStatus(endpointId);
  257. }
  258. }
  259. /**
  260. * Checks if given participant's video is considered frozen.
  261. * @param {JitsiParticipant} participant
  262. * @return {boolean} <tt>true</tt> if the video has frozen for given
  263. * participant or <tt>false</tt> when it's either not considered frozen
  264. * (yet) or if freeze detection is not supported by the current browser.
  265. *
  266. * FIXME merge this logic with NO_DATA_FROM_SOURCE event
  267. * implemented in JitsiLocalTrack by extending the event to
  268. * the remote track and allowing to set different timeout for
  269. * local and remote tracks.
  270. *
  271. */
  272. isVideoTrackFrozen(participant) {
  273. if (!RTCBrowserType.isVideoMuteOnConnInterruptedSupported()) {
  274. return false;
  275. }
  276. const hasAnyVideoRTCMuted = participant.hasAnyVideoTrackWebRTCMuted();
  277. const rtcMutedTimestamp
  278. = this.rtcMutedTimestamp[participant.getId()];
  279. return hasAnyVideoRTCMuted
  280. && typeof rtcMutedTimestamp === 'number'
  281. && (Date.now() - rtcMutedTimestamp) >= this.rtcMuteTimeout;
  282. }
  283. /**
  284. * Goes over every participant and updates connectivity status.
  285. * Should be called when a parameter which affects all of the participants
  286. * is changed (P2P for example).
  287. */
  288. refreshConnectionStatusForAll() {
  289. const participants = this.conference.getParticipants();
  290. for (const participant of participants) {
  291. this.figureOutConnectionStatus(participant.getId());
  292. }
  293. }
  294. /**
  295. * Figures out (and updates) the current connectivity status for
  296. * the participant identified by the given id.
  297. *
  298. * @param {string} id the participant's id (MUC nickname or Colibri endpoint
  299. * ID).
  300. */
  301. figureOutConnectionStatus(id) {
  302. const participant = this.conference.getParticipantById(id);
  303. if (!participant) {
  304. // Probably the participant is no longer in the conference
  305. // (at the time of writing this code, participant is
  306. // detached from the conference and TRACK_REMOVED events are
  307. // fired),
  308. // so we don't care, but let's print the warning for
  309. // debugging purpose
  310. logger.warn(`figure out conn status - no participant for: ${id}`);
  311. return;
  312. }
  313. const isVideoMuted = participant.isVideoMuted();
  314. const isVideoTrackFrozen = this.isVideoTrackFrozen(participant);
  315. const isInLastN = this.rtc.isInLastN(id);
  316. let isConnActiveByJvb = this.connStatusFromJvb[id];
  317. if (this.conference.isP2PActive()) {
  318. logger.debug('Assuming connection active by JVB - in P2P mode');
  319. isConnActiveByJvb = true;
  320. } else if (typeof isConnActiveByJvb !== 'boolean') {
  321. // If no status was received from the JVB it means that it's active
  322. // (the bridge does not send notification unless there is a problem)
  323. logger.debug('Assuming connection active by JVB - no notification');
  324. isConnActiveByJvb = true;
  325. }
  326. const isConnectionActive
  327. = isConnActiveByJvb
  328. && (isVideoMuted || (isInLastN && !isVideoTrackFrozen));
  329. logger.debug(
  330. `Figure out conn status, is video muted: ${isVideoMuted
  331. } is active(jvb): ${isConnActiveByJvb
  332. } video track frozen: ${isVideoTrackFrozen
  333. } is in last N: ${isInLastN
  334. } => ${isConnectionActive}`);
  335. this._changeConnectionStatus(participant, isConnectionActive);
  336. }
  337. /**
  338. * Handles RTC 'onmute' event for the video track.
  339. *
  340. * @param {JitsiRemoteTrack} track the video track for which 'onmute' event
  341. * will be processed.
  342. */
  343. onTrackRtcMuted(track) {
  344. const participantId = track.getParticipantId();
  345. const participant = this.conference.getParticipantById(participantId);
  346. logger.debug(`Detector track RTC muted: ${participantId}`);
  347. if (!participant) {
  348. logger.error(`No participant for id: ${participantId}`);
  349. return;
  350. }
  351. this.rtcMutedTimestamp[participantId] = Date.now();
  352. if (!participant.isVideoMuted()) {
  353. // If the user is not muted according to the signalling we'll give
  354. // it some time, before the connection interrupted event is
  355. // triggered.
  356. this.clearTimeout(participantId);
  357. this.trackTimers[participantId] = window.setTimeout(() => {
  358. logger.debug(`RTC mute timeout for: ${participantId}`);
  359. this.clearTimeout(participantId);
  360. this.figureOutConnectionStatus(participantId);
  361. }, this.rtcMuteTimeout);
  362. }
  363. }
  364. /**
  365. * Handles RTC 'onunmute' event for the video track.
  366. *
  367. * @param {JitsiRemoteTrack} track the video track for which 'onunmute'
  368. * event will be processed.
  369. */
  370. onTrackRtcUnmuted(track) {
  371. const participantId = track.getParticipantId();
  372. logger.debug(`Detector track RTC unmuted: ${participantId}`);
  373. this.clearTimeout(participantId);
  374. this.clearRtcMutedTimestamp(participantId);
  375. this.figureOutConnectionStatus(participantId);
  376. }
  377. /**
  378. * Here the signalling "mute"/"unmute" events are processed.
  379. *
  380. * @param {JitsiRemoteTrack} track the remote video track for which
  381. * the signalling mute/unmute event will be processed.
  382. */
  383. onSignallingMuteChanged(track) {
  384. const participantId = track.getParticipantId();
  385. logger.debug(
  386. `Detector on track signalling mute changed: ${participantId}`,
  387. track.isMuted());
  388. this.figureOutConnectionStatus(participantId);
  389. }
  390. }