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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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(participantId => {
  142. this.clearTimeout(participantId);
  143. this.clearRtcMutedTimestamp(participantId);
  144. });
  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(${Date.now()}): ${
  158. 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. /**
  167. *
  168. * @param participant
  169. * @param newStatus
  170. */
  171. _changeConnectionStatus(participant, newStatus) {
  172. if (participant.isConnectionActive() !== newStatus) {
  173. const endpointId = participant.getId();
  174. participant._setIsConnectionActive(newStatus);
  175. logger.debug(
  176. `Emit endpoint conn status(${Date.now()}) ${endpointId}: ${
  177. newStatus}`);
  178. // Log the event on CallStats
  179. Statistics.sendLog(
  180. JSON.stringify({
  181. id: 'peer.conn.status',
  182. participant: endpointId,
  183. status: newStatus
  184. }));
  185. // and analytics
  186. Statistics.analytics.sendEvent('peer.conn.status',
  187. { label: newStatus });
  188. this.conference.eventEmitter.emit(
  189. JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED,
  190. endpointId, newStatus);
  191. }
  192. }
  193. /**
  194. * Reset the postponed "connection interrupted" event which was previously
  195. * scheduled as a timeout on RTC 'onmute' event.
  196. *
  197. * @param participantId the participant for which the "connection
  198. * interrupted" timeout was scheduled
  199. */
  200. clearTimeout(participantId) {
  201. if (this.trackTimers[participantId]) {
  202. window.clearTimeout(this.trackTimers[participantId]);
  203. this.trackTimers[participantId] = null;
  204. }
  205. }
  206. /**
  207. * Clears the timestamp of the RTC muted event for participant's video track
  208. * @param participantId the id of the conference participant which is
  209. * the same as the Colibri endpoint ID of the video channel allocated for
  210. * the user on the videobridge.
  211. */
  212. clearRtcMutedTimestamp(participantId) {
  213. this.rtcMutedTimestamp[participantId] = null;
  214. }
  215. /**
  216. * Bind signalling mute event listeners for video {JitsiRemoteTrack} when
  217. * a new one is added to the conference.
  218. *
  219. * @param {JitsiTrack} remoteTrack the {JitsiTrack} which is being added to
  220. * the conference.
  221. */
  222. onRemoteTrackAdded(remoteTrack) {
  223. if (!remoteTrack.isLocal()
  224. && remoteTrack.getType() === MediaType.VIDEO) {
  225. logger.debug(
  226. `Detector on remote track added for: ${
  227. remoteTrack.getParticipantId()}`);
  228. remoteTrack.on(
  229. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  230. this._onSignallingMuteChanged);
  231. }
  232. }
  233. /**
  234. * Removes all event listeners bound to the remote video track and clears
  235. * any related timeouts.
  236. *
  237. * @param {JitsiRemoteTrack} remoteTrack the remote track which is being
  238. * removed from the conference.
  239. */
  240. onRemoteTrackRemoved(remoteTrack) {
  241. if (!remoteTrack.isLocal()
  242. && remoteTrack.getType() === MediaType.VIDEO) {
  243. const endpointId = remoteTrack.getParticipantId();
  244. logger.debug(`Detector on remote track removed: ${endpointId}`);
  245. remoteTrack.off(
  246. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  247. this._onSignallingMuteChanged);
  248. this.clearTimeout(endpointId);
  249. this.clearRtcMutedTimestamp(endpointId);
  250. this.figureOutConnectionStatus(endpointId);
  251. }
  252. }
  253. /**
  254. * Checks if given participant's video is considered frozen.
  255. * @param {JitsiParticipant} participant
  256. * @return {boolean} <tt>true</tt> if the video has frozen for given
  257. * participant or <tt>false</tt> when it's either not considered frozen
  258. * (yet) or if freeze detection is not supported by the current browser.
  259. *
  260. * FIXME merge this logic with NO_DATA_FROM_SOURCE event
  261. * implemented in JitsiLocalTrack by extending the event to
  262. * the remote track and allowing to set different timeout for
  263. * local and remote tracks.
  264. *
  265. */
  266. isVideoTrackFrozen(participant) {
  267. if (!RTCBrowserType.isVideoMuteOnConnInterruptedSupported()) {
  268. return false;
  269. }
  270. const hasAnyVideoRTCMuted = participant.hasAnyVideoTrackWebRTCMuted();
  271. const rtcMutedTimestamp
  272. = this.rtcMutedTimestamp[participant.getId()];
  273. return hasAnyVideoRTCMuted
  274. && typeof rtcMutedTimestamp === 'number'
  275. && (Date.now() - rtcMutedTimestamp) >= this.rtcMuteTimeout;
  276. }
  277. /**
  278. * Figures out (and updates) the current connectivity status for
  279. * the participant identified by the given id.
  280. *
  281. * @param {string} id the participant's id (MUC nickname or Colibri endpoint
  282. * ID).
  283. */
  284. figureOutConnectionStatus(id) {
  285. const participant = this.conference.getParticipantById(id);
  286. if (!participant) {
  287. // Probably the participant is no longer in the conference
  288. // (at the time of writing this code, participant is
  289. // detached from the conference and TRACK_REMOVED events are
  290. // fired),
  291. // so we don't care, but let's print the warning for
  292. // debugging purpose
  293. logger.warn(`figure out conn status - no participant for: ${id}`);
  294. return;
  295. }
  296. const isVideoMuted = participant.isVideoMuted();
  297. const isVideoTrackFrozen = this.isVideoTrackFrozen(participant);
  298. let isConnActiveByJvb = this.connStatusFromJvb[id];
  299. // If no status was received from the JVB it means that it's active
  300. // (the bridge does not send notification unless there is a problem).
  301. if (typeof isConnActiveByJvb !== 'boolean') {
  302. logger.debug('Assuming connection active by JVB - no notification');
  303. isConnActiveByJvb = true;
  304. }
  305. const isConnectionActive
  306. = isConnActiveByJvb && (isVideoMuted || !isVideoTrackFrozen);
  307. logger.debug(
  308. `Figure out conn status, is video muted: ${isVideoMuted
  309. } is active(jvb): ${isConnActiveByJvb
  310. } video track frozen: ${isVideoTrackFrozen
  311. } => ${isConnectionActive}`);
  312. this._changeConnectionStatus(participant, isConnectionActive);
  313. }
  314. /**
  315. * Handles RTC 'onmute' event for the video track.
  316. *
  317. * @param {JitsiRemoteTrack} track the video track for which 'onmute' event
  318. * will be processed.
  319. */
  320. onTrackRtcMuted(track) {
  321. const participantId = track.getParticipantId();
  322. const participant = this.conference.getParticipantById(participantId);
  323. logger.debug(`Detector track RTC muted: ${participantId}`);
  324. if (!participant) {
  325. logger.error(`No participant for id: ${participantId}`);
  326. return;
  327. }
  328. this.rtcMutedTimestamp[participantId] = Date.now();
  329. if (!participant.isVideoMuted()) {
  330. // If the user is not muted according to the signalling we'll give
  331. // it some time, before the connection interrupted event is
  332. // triggered.
  333. this.clearTimeout(participantId);
  334. this.trackTimers[participantId] = window.setTimeout(() => {
  335. logger.debug(`RTC mute timeout for: ${participantId}`);
  336. this.clearTimeout(participantId);
  337. this.figureOutConnectionStatus(participantId);
  338. }, this.rtcMuteTimeout);
  339. }
  340. }
  341. /**
  342. * Handles RTC 'onunmute' event for the video track.
  343. *
  344. * @param {JitsiRemoteTrack} track the video track for which 'onunmute'
  345. * event will be processed.
  346. */
  347. onTrackRtcUnmuted(track) {
  348. const participantId = track.getParticipantId();
  349. logger.debug(`Detector track RTC unmuted: ${participantId}`);
  350. this.clearTimeout(participantId);
  351. this.clearRtcMutedTimestamp(participantId);
  352. this.figureOutConnectionStatus(participantId);
  353. }
  354. /**
  355. * Here the signalling "mute"/"unmute" events are processed.
  356. *
  357. * @param {JitsiRemoteTrack} track the remote video track for which
  358. * the signalling mute/unmute event will be processed.
  359. */
  360. onSignallingMuteChanged(track) {
  361. const participantId = track.getParticipantId();
  362. logger.debug(
  363. `Detector on track signalling mute changed: ${participantId}`,
  364. track.isMuted());
  365. this.figureOutConnectionStatus(participantId);
  366. }
  367. }