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

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