Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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. * @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. * Handles RTCEvents.ENDPOINT_CONN_STATUS_CHANGED triggered when we receive
  106. * notification over the data channel from the bridge about endpoint's
  107. * connection status update.
  108. * @param endpointId {string} the endpoint ID(MUC nickname/resource JID)
  109. * @param isActive {boolean} true if the connection is OK or false otherwise
  110. */
  111. ParticipantConnectionStatus.prototype.onEndpointConnStatusChanged
  112. = function(endpointId, isActive) {
  113. logger.debug(
  114. 'Detector RTCEvents.ENDPOINT_CONN_STATUS_CHANGED('
  115. + Date.now() +'): ' + endpointId + ': ' + isActive);
  116. // Filter out events for the local JID for now
  117. if (endpointId !== this.conference.myUserId()) {
  118. var participant = this.conference.getParticipantById(endpointId);
  119. // Delay the 'active' event until the video track gets RTC unmuted event
  120. if (isActive
  121. && RTCBrowserType.isVideoMuteOnConnInterruptedSupported()
  122. && participant
  123. && participant.hasAnyVideoTrackWebRTCMuted()
  124. && !participant.isVideoMuted()) {
  125. logger.debug(
  126. 'Ignoring RTCEvents.ENDPOINT_CONN_STATUS_CHANGED -'
  127. + ' will wait for unmute event');
  128. } else {
  129. this._changeConnectionStatus(endpointId, isActive);
  130. }
  131. }
  132. };
  133. ParticipantConnectionStatus.prototype._changeConnectionStatus
  134. = function (endpointId, newStatus) {
  135. var participant = this.conference.getParticipantById(endpointId);
  136. if (!participant) {
  137. // This will happen when participant exits the conference with broken
  138. // ICE connection and we join after that. The bridge keeps sending
  139. // that notification until the conference does not expire.
  140. logger.warn(
  141. 'Missed participant connection status update - ' +
  142. 'no participant for endpoint: ' + endpointId);
  143. return;
  144. }
  145. if (participant.isConnectionActive() !== newStatus) {
  146. participant._setIsConnectionActive(newStatus);
  147. logger.debug(
  148. 'Emit endpoint conn status(' + Date.now() + '): ',
  149. endpointId, newStatus);
  150. // Log the event on CallStats
  151. Statistics.sendLog(
  152. JSON.stringify({
  153. id: 'peer.conn.status',
  154. participant: endpointId,
  155. status: newStatus
  156. }));
  157. // and analytics
  158. Statistics.analytics.sendEvent('peer.conn.status',
  159. {label: newStatus});
  160. this.conference.eventEmitter.emit(
  161. JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED,
  162. endpointId, newStatus);
  163. }
  164. };
  165. /**
  166. * Reset the postponed "connection interrupted" event which was previously
  167. * scheduled as a timeout on RTC 'onmute' event.
  168. *
  169. * @param participantId the participant for which the "connection interrupted"
  170. * timeout was scheduled
  171. */
  172. ParticipantConnectionStatus.prototype.clearTimeout = function (participantId) {
  173. if (this.trackTimers[participantId]) {
  174. window.clearTimeout(this.trackTimers[participantId]);
  175. this.trackTimers[participantId] = null;
  176. }
  177. };
  178. /**
  179. * Bind signalling mute event listeners for video {JitsiRemoteTrack} when
  180. * a new one is added to the conference.
  181. *
  182. * @param {JitsiTrack} remoteTrack the {JitsiTrack} which is being added to
  183. * the conference.
  184. */
  185. ParticipantConnectionStatus.prototype.onRemoteTrackAdded
  186. = function(remoteTrack) {
  187. if (!remoteTrack.isLocal() && remoteTrack.getType() === MediaType.VIDEO) {
  188. logger.debug(
  189. 'Detector on remote track added: ', remoteTrack.getParticipantId());
  190. remoteTrack.on(
  191. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  192. this._onSignallingMuteChanged);
  193. }
  194. };
  195. /**
  196. * Removes all event listeners bound to the remote video track and clears any
  197. * related timeouts.
  198. *
  199. * @param {JitsiRemoteTrack} remoteTrack the remote track which is being removed
  200. * from the conference.
  201. */
  202. ParticipantConnectionStatus.prototype.onRemoteTrackRemoved
  203. = function(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. ParticipantConnectionStatus.prototype.onTrackRtcMuted = function(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. ParticipantConnectionStatus.prototype.onTrackRtcUnmuted = function(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. ParticipantConnectionStatus.prototype.onSignallingMuteChanged
  268. = function (track) {
  269. logger.debug(
  270. 'Detector on track signalling mute changed: ', track, track.isMuted());
  271. var isMuted = track.isMuted();
  272. var participantId = track.getParticipantId();
  273. var participant = this.conference.getParticipantById(participantId);
  274. if (!participant) {
  275. logger.error('No participant for id: ' + participantId);
  276. return;
  277. }
  278. var isConnectionActive = participant.isConnectionActive();
  279. if (isMuted && isConnectionActive && this.trackTimers[participantId]) {
  280. logger.debug(
  281. 'Signalling got in sync - cancelling task for: ' + participantId);
  282. this.clearTimeout(participantId);
  283. }
  284. };
  285. module.exports = ParticipantConnectionStatus;