Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ParticipantConnectionStatus.js 12KB

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