modified lib-jitsi-meet dev repo
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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. // On some browsers MediaStreamTrack trigger "onmute"/"onunmute"
  94. // events for video type tracks when they stop receiving data which is
  95. // often a sign that remote user is having connectivity issues
  96. if (RTCBrowserType.isVideoMuteOnConnInterruptedSupported()) {
  97. this._onTrackRtcMuted = this.onTrackRtcMuted.bind(this);
  98. this.rtc.addListener(
  99. RTCEvents.REMOTE_TRACK_MUTE, this._onTrackRtcMuted);
  100. this._onTrackRtcUnmuted = this.onTrackRtcUnmuted.bind(this);
  101. this.rtc.addListener(
  102. RTCEvents.REMOTE_TRACK_UNMUTE, this._onTrackRtcUnmuted);
  103. // Track added/removed listeners are used to bind "mute"/"unmute"
  104. // event handlers
  105. this._onRemoteTrackAdded = this.onRemoteTrackAdded.bind(this);
  106. this.conference.on(
  107. JitsiConferenceEvents.TRACK_ADDED,
  108. this._onRemoteTrackAdded);
  109. this._onRemoteTrackRemoved = this.onRemoteTrackRemoved.bind(this);
  110. this.conference.on(
  111. JitsiConferenceEvents.TRACK_REMOVED,
  112. this._onRemoteTrackRemoved);
  113. // Listened which will be bound to JitsiRemoteTrack to listen for
  114. // signalling mute/unmute events.
  115. this._onSignallingMuteChanged
  116. = this.onSignallingMuteChanged.bind(this);
  117. }
  118. }
  119. /**
  120. * Removes all event listeners and disposes of all resources held by this
  121. * instance.
  122. */
  123. dispose() {
  124. this.rtc.removeListener(
  125. RTCEvents.ENDPOINT_CONN_STATUS_CHANGED,
  126. this._onEndpointConnStatusChanged);
  127. if (RTCBrowserType.isVideoMuteOnConnInterruptedSupported()) {
  128. this.rtc.removeListener(
  129. RTCEvents.REMOTE_TRACK_MUTE,
  130. this._onTrackRtcMuted);
  131. this.rtc.removeListener(
  132. RTCEvents.REMOTE_TRACK_UNMUTE,
  133. this._onTrackRtcUnmuted);
  134. this.conference.off(
  135. JitsiConferenceEvents.TRACK_ADDED,
  136. this._onRemoteTrackAdded);
  137. this.conference.off(
  138. JitsiConferenceEvents.TRACK_REMOVED,
  139. this._onRemoteTrackRemoved);
  140. }
  141. Object.keys(this.trackTimers).forEach(function (participantId) {
  142. this.clearTimeout(participantId);
  143. this.clearRtcMutedTimestamp(participantId);
  144. }.bind(this));
  145. // Clear RTC connection status cache
  146. this.connStatusFromJvb = {};
  147. }
  148. /**
  149. * Handles RTCEvents.ENDPOINT_CONN_STATUS_CHANGED triggered when we receive
  150. * notification over the data channel from the bridge about endpoint's
  151. * connection status update.
  152. * @param endpointId {string} the endpoint ID(MUC nickname/resource JID)
  153. * @param isActive {boolean} true if the connection is OK or false otherwise
  154. */
  155. onEndpointConnStatusChanged(endpointId, isActive) {
  156. logger.debug(
  157. 'Detector RTCEvents.ENDPOINT_CONN_STATUS_CHANGED('
  158. + Date.now() +'): ' + endpointId + ': ' + isActive);
  159. // Filter out events for the local JID for now
  160. if (endpointId !== this.conference.myUserId()) {
  161. // Store the status received over the data channels
  162. this.connStatusFromJvb[endpointId] = isActive;
  163. this.figureOutConnectionStatus(endpointId);
  164. }
  165. }
  166. _changeConnectionStatus(participant, newStatus) {
  167. if (participant.isConnectionActive() !== newStatus) {
  168. const endpointId = participant.getId();
  169. participant._setIsConnectionActive(newStatus);
  170. logger.debug(
  171. 'Emit endpoint conn status(' + Date.now() + ') '
  172. + endpointId + ": " + newStatus);
  173. // Log the event on CallStats
  174. Statistics.sendLog(
  175. JSON.stringify({
  176. id: 'peer.conn.status',
  177. participant: endpointId,
  178. status: newStatus
  179. }));
  180. // and analytics
  181. Statistics.analytics.sendEvent('peer.conn.status',
  182. {label: newStatus});
  183. this.conference.eventEmitter.emit(
  184. JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED,
  185. endpointId, newStatus);
  186. }
  187. }
  188. /**
  189. * Reset the postponed "connection interrupted" event which was previously
  190. * scheduled as a timeout on RTC 'onmute' event.
  191. *
  192. * @param participantId the participant for which the "connection
  193. * interrupted" timeout was scheduled
  194. */
  195. clearTimeout(participantId) {
  196. if (this.trackTimers[participantId]) {
  197. window.clearTimeout(this.trackTimers[participantId]);
  198. this.trackTimers[participantId] = null;
  199. }
  200. }
  201. /**
  202. * Clears the timestamp of the RTC muted event for participant's video track
  203. * @param participantId the id of the conference participant which is
  204. * the same as the Colibri endpoint ID of the video channel allocated for
  205. * the user on the videobridge.
  206. */
  207. clearRtcMutedTimestamp(participantId) {
  208. this.rtcMutedTimestamp[participantId] = null;
  209. }
  210. /**
  211. * Bind signalling mute event listeners for video {JitsiRemoteTrack} when
  212. * a new one is added to the conference.
  213. *
  214. * @param {JitsiTrack} remoteTrack the {JitsiTrack} which is being added to
  215. * the conference.
  216. */
  217. onRemoteTrackAdded(remoteTrack) {
  218. if (!remoteTrack.isLocal()
  219. && remoteTrack.getType() === MediaType.VIDEO) {
  220. logger.debug(
  221. 'Detector on remote track added for: '
  222. + remoteTrack.getParticipantId());
  223. remoteTrack.on(
  224. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  225. this._onSignallingMuteChanged);
  226. }
  227. }
  228. /**
  229. * Removes all event listeners bound to the remote video track and clears
  230. * any related timeouts.
  231. *
  232. * @param {JitsiRemoteTrack} remoteTrack the remote track which is being
  233. * removed from the conference.
  234. */
  235. onRemoteTrackRemoved(remoteTrack) {
  236. if (!remoteTrack.isLocal()
  237. && remoteTrack.getType() === MediaType.VIDEO) {
  238. const endpointId = remoteTrack.getParticipantId();
  239. logger.debug(
  240. 'Detector on remote track removed: ' + endpointId);
  241. remoteTrack.off(
  242. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  243. this._onSignallingMuteChanged);
  244. this.clearTimeout(endpointId);
  245. this.clearRtcMutedTimestamp(endpointId);
  246. this.figureOutConnectionStatus(endpointId);
  247. }
  248. }
  249. /**
  250. * Checks if given participant's video is considered frozen.
  251. * @param {JitsiParticipant} participant
  252. * @return {boolean} <tt>true</tt> if the video has frozen for given
  253. * participant or <tt>false</tt> when it's either not considered frozen
  254. * (yet) or if freeze detection is not supported by the current browser.
  255. *
  256. * FIXME merge this logic with NO_DATA_FROM_SOURCE event
  257. * implemented in JitsiLocalTrack by extending the event to
  258. * the remote track and allowing to set different timeout for
  259. * local and remote tracks.
  260. *
  261. */
  262. isVideoTrackFrozen (participant) {
  263. if (!RTCBrowserType.isVideoMuteOnConnInterruptedSupported()) {
  264. return false;
  265. }
  266. const hasAnyVideoRTCMuted = participant.hasAnyVideoTrackWebRTCMuted();
  267. const rtcMutedTimestamp
  268. = this.rtcMutedTimestamp[participant.getId()];
  269. return hasAnyVideoRTCMuted
  270. && typeof rtcMutedTimestamp === 'number'
  271. && (Date.now() - rtcMutedTimestamp) >= this.rtcMuteTimeout;
  272. }
  273. /**
  274. * Figures out (and updates) the current connectivity status for
  275. * the participant identified by the given id.
  276. *
  277. * @param {string} id the participant's id (MUC nickname or Colibri endpoint
  278. * ID).
  279. */
  280. figureOutConnectionStatus(id) {
  281. const participant = this.conference.getParticipantById(id);
  282. if (!participant) {
  283. // Probably the participant is no longer in the conference
  284. // (at the time of writing this code, participant is
  285. // detached from the conference and TRACK_REMOVED events are
  286. // fired),
  287. // so we don't care, but let's print the warning for
  288. // debugging purpose
  289. logger.warn('figure out conn status - no participant for: ' + id);
  290. return;
  291. }
  292. const isVideoMuted = participant.isVideoMuted();
  293. const isVideoTrackFrozen = this.isVideoTrackFrozen(participant);
  294. let isConnActiveByJvb = this.connStatusFromJvb[id];
  295. // If no status was received from the JVB it means that it's active
  296. // (the bridge does not send notification unless there is a problem).
  297. if (typeof isConnActiveByJvb !== 'boolean') {
  298. logger.debug('Assuming connection active by JVB - no notification');
  299. isConnActiveByJvb = true;
  300. }
  301. const isConnectionActive
  302. = isConnActiveByJvb && (isVideoMuted || !isVideoTrackFrozen);
  303. logger.debug(
  304. 'Figure out conn status, is video muted: ' + isVideoMuted
  305. + ' is active(jvb): ' + isConnActiveByJvb
  306. + ' video track frozen: ' + isVideoTrackFrozen
  307. + ' => ' + isConnectionActive);
  308. this._changeConnectionStatus(participant, isConnectionActive);
  309. }
  310. /**
  311. * Handles RTC 'onmute' event for the video track.
  312. *
  313. * @param {JitsiRemoteTrack} track the video track for which 'onmute' event
  314. * will be processed.
  315. */
  316. onTrackRtcMuted(track) {
  317. const participantId = track.getParticipantId();
  318. const participant = this.conference.getParticipantById(participantId);
  319. logger.debug('Detector track RTC muted: ' + participantId);
  320. if (!participant) {
  321. logger.error('No participant for id: ' + participantId);
  322. return;
  323. }
  324. this.rtcMutedTimestamp[participantId] = Date.now();
  325. if (!participant.isVideoMuted()) {
  326. // If the user is not muted according to the signalling we'll give
  327. // it some time, before the connection interrupted event is
  328. // triggered.
  329. this.clearTimeout(participantId);
  330. this.trackTimers[participantId] = window.setTimeout(function () {
  331. logger.debug('RTC mute timeout for: ' + participantId);
  332. this.clearTimeout(participantId);
  333. this.figureOutConnectionStatus(participantId);
  334. }.bind(this), this.rtcMuteTimeout);
  335. }
  336. }
  337. /**
  338. * Handles RTC 'onunmute' event for the video track.
  339. *
  340. * @param {JitsiRemoteTrack} track the video track for which 'onunmute'
  341. * event will be processed.
  342. */
  343. onTrackRtcUnmuted(track) {
  344. const participantId = track.getParticipantId();
  345. logger.debug('Detector track RTC unmuted: ' + participantId);
  346. this.clearTimeout(participantId);
  347. this.clearRtcMutedTimestamp(participantId);
  348. this.figureOutConnectionStatus(participantId);
  349. }
  350. /**
  351. * Here the signalling "mute"/"unmute" events are processed.
  352. *
  353. * @param {JitsiRemoteTrack} track the remote video track for which
  354. * the signalling mute/unmute event will be processed.
  355. */
  356. onSignallingMuteChanged (track) {
  357. const participantId = track.getParticipantId();
  358. logger.debug(
  359. 'Detector on track signalling mute changed: '
  360. + participantId, track.isMuted());
  361. this.figureOutConnectionStatus(participantId);
  362. }
  363. }