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

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