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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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',
  175. {label: newStatus});
  176. this.conference.eventEmitter.emit(
  177. JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED,
  178. endpointId, newStatus);
  179. }
  180. };
  181. /**
  182. * Reset the postponed "connection interrupted" event which was previously
  183. * scheduled as a timeout on RTC 'onmute' event.
  184. *
  185. * @param participantId the participant for which the "connection interrupted"
  186. * timeout was scheduled
  187. */
  188. ParticipantConnectionStatus.prototype.clearTimeout = function (participantId) {
  189. if (this.trackTimers[participantId]) {
  190. window.clearTimeout(this.trackTimers[participantId]);
  191. this.trackTimers[participantId] = null;
  192. }
  193. };
  194. /**
  195. * Bind signalling mute event listeners for video {JitsiRemoteTrack} when
  196. * a new one is added to the conference.
  197. *
  198. * @param {JitsiTrack} remoteTrack the {JitsiTrack} which is being added to
  199. * the conference.
  200. */
  201. ParticipantConnectionStatus.prototype.onRemoteTrackAdded
  202. = function(remoteTrack) {
  203. if (!remoteTrack.isLocal() && remoteTrack.getType() === MediaType.VIDEO) {
  204. logger.debug(
  205. 'Detector on remote track added: ', remoteTrack.getParticipantId());
  206. remoteTrack.on(
  207. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  208. this._onSignallingMuteChanged);
  209. }
  210. };
  211. /**
  212. * Removes all event listeners bound to the remote video track and clears any
  213. * related timeouts.
  214. *
  215. * @param {JitsiRemoteTrack} remoteTrack the remote track which is being removed
  216. * from the conference.
  217. */
  218. ParticipantConnectionStatus.prototype.onRemoteTrackRemoved
  219. = function(remoteTrack) {
  220. if (!remoteTrack.isLocal() && remoteTrack.getType() === MediaType.VIDEO) {
  221. logger.debug(
  222. 'Detector on remote track removed: ',
  223. remoteTrack.getParticipantId());
  224. remoteTrack.off(
  225. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  226. this._onSignallingMuteChanged);
  227. this.clearTimeout(remoteTrack.getParticipantId());
  228. }
  229. };
  230. /**
  231. * Handles RTC 'onmute' event for the video track.
  232. *
  233. * @param {JitsiRemoteTrack} track the video track for which 'onmute' event will
  234. * be processed.
  235. */
  236. ParticipantConnectionStatus.prototype.onTrackRtcMuted = function(track) {
  237. var participantId = track.getParticipantId();
  238. var participant = this.conference.getParticipantById(participantId);
  239. logger.debug('Detector track RTC muted: ', participantId);
  240. if (!participant) {
  241. logger.error('No participant for id: ' + participantId);
  242. return;
  243. }
  244. if (!participant.isVideoMuted()) {
  245. // If the user is not muted according to the signalling we'll give it
  246. // some time, before the connection interrupted event is triggered.
  247. this.trackTimers[participantId] = window.setTimeout(function () {
  248. if (!track.isMuted() && participant.isConnectionActive()) {
  249. logger.info(
  250. 'Connection interrupted through the RTC mute: '
  251. + participantId, Date.now());
  252. this._changeConnectionStatus(participantId, false);
  253. }
  254. this.clearTimeout(participantId);
  255. }.bind(this), this.rtcMuteTimeout);
  256. }
  257. };
  258. /**
  259. * Handles RTC 'onunmute' event for the video track.
  260. *
  261. * @param {JitsiRemoteTrack} track the video track for which 'onunmute' event
  262. * will be processed.
  263. */
  264. ParticipantConnectionStatus.prototype.onTrackRtcUnmuted = function(track) {
  265. logger.debug('Detector track RTC unmuted: ', track);
  266. var participantId = track.getParticipantId();
  267. if (!track.isMuted() &&
  268. !this.conference.getParticipantById(participantId)
  269. .isConnectionActive()) {
  270. logger.info(
  271. 'Detector connection restored through the RTC unmute: '
  272. + participantId, Date.now());
  273. this._changeConnectionStatus(participantId, true);
  274. }
  275. this.clearTimeout(participantId);
  276. };
  277. /**
  278. * Here the signalling "mute"/"unmute" events are processed.
  279. *
  280. * @param {JitsiRemoteTrack} track the remote video track for which
  281. * the signalling mute/unmute event will be processed.
  282. */
  283. ParticipantConnectionStatus.prototype.onSignallingMuteChanged
  284. = function (track) {
  285. logger.debug(
  286. 'Detector on track signalling mute changed: ', track, track.isMuted());
  287. var isMuted = track.isMuted();
  288. var participantId = track.getParticipantId();
  289. var participant = this.conference.getParticipantById(participantId);
  290. if (!participant) {
  291. logger.error('No participant for id: ' + participantId);
  292. return;
  293. }
  294. var isConnectionActive = participant.isConnectionActive();
  295. if (isMuted && isConnectionActive && this.trackTimers[participantId]) {
  296. logger.debug(
  297. 'Signalling got in sync - cancelling task for: ' + participantId);
  298. this.clearTimeout(participantId);
  299. }
  300. };
  301. module.exports = ParticipantConnectionStatus;