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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* global __filename, module, require */
  2. var logger = require('jitsi-meet-logger').getLogger(__filename);
  3. var MediaType = require('../../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 of MUC
  35. * JID(nickname)) to the timeout callback IDs scheduled using
  36. * window.setTimeout.
  37. * @type {Object.<string, number>}
  38. */
  39. this.trackTimers = {};
  40. /**
  41. * How long we're going to wait after the RTC video track muted event for
  42. * the corresponding signalling mute event, before the connection
  43. * interrupted is fired. The default value is
  44. * {@link DEFAULT_RTC_MUTE_TIMEOUT}.
  45. *
  46. * @type {number} amount of time in milliseconds
  47. */
  48. this.rtcMuteTimeout
  49. = typeof rtcMuteTimeout === 'number'
  50. ? rtcMuteTimeout : DEFAULT_RTC_MUTE_TIMEOUT;
  51. logger.info("RtcMuteTimeout set to: " + this.rtcMuteTimeout);
  52. }
  53. /**
  54. * Initializes <tt>ParticipantConnectionStatus</tt> and bind required event
  55. * listeners.
  56. */
  57. init() {
  58. this._onEndpointConnStatusChanged
  59. = this.onEndpointConnStatusChanged.bind(this);
  60. this.rtc.addListener(
  61. RTCEvents.ENDPOINT_CONN_STATUS_CHANGED,
  62. this._onEndpointConnStatusChanged);
  63. // On some browsers MediaStreamTrack trigger "onmute"/"onunmute"
  64. // events for video type tracks when they stop receiving data which is
  65. // often a sign that remote user is having connectivity issues
  66. if (RTCBrowserType.isVideoMuteOnConnInterruptedSupported()) {
  67. this._onTrackRtcMuted = this.onTrackRtcMuted.bind(this);
  68. this.rtc.addListener(
  69. RTCEvents.REMOTE_TRACK_MUTE, this._onTrackRtcMuted);
  70. this._onTrackRtcUnmuted = this.onTrackRtcUnmuted.bind(this);
  71. this.rtc.addListener(
  72. RTCEvents.REMOTE_TRACK_UNMUTE, this._onTrackRtcUnmuted);
  73. // Track added/removed listeners are used to bind "mute"/"unmute"
  74. // event handlers
  75. this._onRemoteTrackAdded = this.onRemoteTrackAdded.bind(this);
  76. this.conference.on(
  77. JitsiConferenceEvents.TRACK_ADDED, this._onRemoteTrackAdded);
  78. this._onRemoteTrackRemoved = this.onRemoteTrackRemoved.bind(this);
  79. this.conference.on(
  80. JitsiConferenceEvents.TRACK_REMOVED, this._onRemoteTrackRemoved);
  81. // Listened which will be bound to JitsiRemoteTrack to listen for
  82. // signalling mute/unmute events.
  83. this._onSignallingMuteChanged = this.onSignallingMuteChanged.bind(this);
  84. }
  85. }
  86. /**
  87. * Removes all event listeners and disposes of all resources held by this
  88. * instance.
  89. */
  90. dispose() {
  91. this.rtc.removeListener(
  92. RTCEvents.ENDPOINT_CONN_STATUS_CHANGED,
  93. this._onEndpointConnStatusChanged);
  94. if (RTCBrowserType.isVideoMuteOnConnInterruptedSupported()) {
  95. this.rtc.removeListener(
  96. RTCEvents.REMOTE_TRACK_MUTE, this._onTrackRtcMuted);
  97. this.rtc.removeListener(
  98. RTCEvents.REMOTE_TRACK_UNMUTE, this._onTrackRtcUnmuted);
  99. this.conference.off(
  100. JitsiConferenceEvents.TRACK_ADDED, this._onRemoteTrackAdded);
  101. this.conference.off(
  102. JitsiConferenceEvents.TRACK_REMOVED, this._onRemoteTrackRemoved);
  103. }
  104. Object.keys(this.trackTimers).forEach(function (participantId) {
  105. this.clearTimeout(participantId);
  106. }.bind(this));
  107. }
  108. /**
  109. * Handles RTCEvents.ENDPOINT_CONN_STATUS_CHANGED triggered when we receive
  110. * notification over the data channel from the bridge about endpoint's
  111. * connection status update.
  112. * @param endpointId {string} the endpoint ID(MUC nickname/resource JID)
  113. * @param isActive {boolean} true if the connection is OK or false otherwise
  114. */
  115. onEndpointConnStatusChanged(endpointId, isActive) {
  116. logger.debug(
  117. 'Detector RTCEvents.ENDPOINT_CONN_STATUS_CHANGED('
  118. + Date.now() +'): ' + endpointId + ': ' + isActive);
  119. // Filter out events for the local JID for now
  120. if (endpointId !== this.conference.myUserId()) {
  121. var participant = this.conference.getParticipantById(endpointId);
  122. // Delay the 'active' event until the video track gets RTC unmuted event
  123. if (isActive
  124. && RTCBrowserType.isVideoMuteOnConnInterruptedSupported()
  125. && participant
  126. && participant.hasAnyVideoTrackWebRTCMuted()
  127. && !participant.isVideoMuted()) {
  128. logger.debug(
  129. 'Ignoring RTCEvents.ENDPOINT_CONN_STATUS_CHANGED -'
  130. + ' will wait for unmute event');
  131. } else {
  132. this._changeConnectionStatus(endpointId, isActive);
  133. }
  134. }
  135. }
  136. _changeConnectionStatus(endpointId, newStatus) {
  137. var participant = this.conference.getParticipantById(endpointId);
  138. if (!participant) {
  139. // This will happen when participant exits the conference with broken
  140. // ICE connection and we join after that. The bridge keeps sending
  141. // that notification until the conference does not expire.
  142. logger.warn(
  143. 'Missed participant connection status update - ' +
  144. 'no participant for endpoint: ' + endpointId);
  145. return;
  146. }
  147. if (participant.isConnectionActive() !== newStatus) {
  148. participant._setIsConnectionActive(newStatus);
  149. logger.debug(
  150. 'Emit endpoint conn status(' + Date.now() + '): ',
  151. endpointId, newStatus);
  152. // Log the event on CallStats
  153. Statistics.sendLog(
  154. JSON.stringify({
  155. id: 'peer.conn.status',
  156. participant: endpointId,
  157. status: newStatus
  158. }));
  159. // and analytics
  160. Statistics.analytics.sendEvent('peer.conn.status',
  161. {label: newStatus});
  162. this.conference.eventEmitter.emit(
  163. JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED,
  164. endpointId, newStatus);
  165. }
  166. }
  167. /**
  168. * Reset the postponed "connection interrupted" event which was previously
  169. * scheduled as a timeout on RTC 'onmute' event.
  170. *
  171. * @param participantId the participant for which the "connection interrupted"
  172. * timeout was scheduled
  173. */
  174. clearTimeout(participantId) {
  175. if (this.trackTimers[participantId]) {
  176. window.clearTimeout(this.trackTimers[participantId]);
  177. this.trackTimers[participantId] = null;
  178. }
  179. }
  180. /**
  181. * Bind signalling mute event listeners for video {JitsiRemoteTrack} when
  182. * a new one is added to the conference.
  183. *
  184. * @param {JitsiTrack} remoteTrack the {JitsiTrack} which is being added to
  185. * the conference.
  186. */
  187. onRemoteTrackAdded(remoteTrack) {
  188. if (!remoteTrack.isLocal() && remoteTrack.getType() === MediaType.VIDEO) {
  189. logger.debug(
  190. 'Detector on remote track added: ', remoteTrack.getParticipantId());
  191. remoteTrack.on(
  192. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  193. this._onSignallingMuteChanged);
  194. }
  195. }
  196. /**
  197. * Removes all event listeners bound to the remote video track and clears any
  198. * related timeouts.
  199. *
  200. * @param {JitsiRemoteTrack} remoteTrack the remote track which is being removed
  201. * from the conference.
  202. */
  203. onRemoteTrackRemoved(remoteTrack) {
  204. if (!remoteTrack.isLocal() && remoteTrack.getType() === MediaType.VIDEO) {
  205. logger.debug(
  206. 'Detector on remote track removed: ',
  207. remoteTrack.getParticipantId());
  208. remoteTrack.off(
  209. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  210. this._onSignallingMuteChanged);
  211. this.clearTimeout(remoteTrack.getParticipantId());
  212. }
  213. }
  214. /**
  215. * Handles RTC 'onmute' event for the video track.
  216. *
  217. * @param {JitsiRemoteTrack} track the video track for which 'onmute' event will
  218. * be processed.
  219. */
  220. onTrackRtcMuted(track) {
  221. var participantId = track.getParticipantId();
  222. var participant = this.conference.getParticipantById(participantId);
  223. logger.debug('Detector track RTC muted: ', participantId);
  224. if (!participant) {
  225. logger.error('No participant for id: ' + participantId);
  226. return;
  227. }
  228. if (!participant.isVideoMuted()) {
  229. // If the user is not muted according to the signalling we'll give it
  230. // some time, before the connection interrupted event is triggered.
  231. this.trackTimers[participantId] = window.setTimeout(function () {
  232. if (!track.isMuted() && participant.isConnectionActive()) {
  233. logger.info(
  234. 'Connection interrupted through the RTC mute: '
  235. + participantId, Date.now());
  236. this._changeConnectionStatus(participantId, false);
  237. }
  238. this.clearTimeout(participantId);
  239. }.bind(this), this.rtcMuteTimeout);
  240. }
  241. }
  242. /**
  243. * Handles RTC 'onunmute' event for the video track.
  244. *
  245. * @param {JitsiRemoteTrack} track the video track for which 'onunmute' event
  246. * will be processed.
  247. */
  248. onTrackRtcUnmuted(track) {
  249. logger.debug('Detector track RTC unmuted: ', track);
  250. var participantId = track.getParticipantId();
  251. if (!track.isMuted() &&
  252. !this.conference.getParticipantById(participantId)
  253. .isConnectionActive()) {
  254. logger.info(
  255. 'Detector connection restored through the RTC unmute: '
  256. + participantId, Date.now());
  257. this._changeConnectionStatus(participantId, true);
  258. }
  259. this.clearTimeout(participantId);
  260. }
  261. /**
  262. * Here the signalling "mute"/"unmute" events are processed.
  263. *
  264. * @param {JitsiRemoteTrack} track the remote video track for which
  265. * the signalling mute/unmute event will be processed.
  266. */
  267. onSignallingMuteChanged (track) {
  268. logger.debug(
  269. 'Detector on track signalling mute changed: ', track, track.isMuted());
  270. var isMuted = track.isMuted();
  271. var participantId = track.getParticipantId();
  272. var participant = this.conference.getParticipantById(participantId);
  273. if (!participant) {
  274. logger.error('No participant for id: ' + participantId);
  275. return;
  276. }
  277. var isConnectionActive = participant.isConnectionActive();
  278. if (isMuted && isConnectionActive && this.trackTimers[participantId]) {
  279. logger.debug(
  280. 'Signalling got in sync - cancelling task for: ' + participantId);
  281. this.clearTimeout(participantId);
  282. }
  283. }
  284. }