您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ParticipantConnectionStatus.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /* global __filename */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  4. import * as JitsiTrackEvents from '../../JitsiTrackEvents';
  5. import * as MediaType from '../../service/RTC/MediaType';
  6. import RTCBrowserType from '../RTC/RTCBrowserType';
  7. import RTCEvents from '../../service/RTC/RTCEvents';
  8. import Statistics from '../statistics/statistics';
  9. const logger = getLogger(__filename);
  10. /**
  11. * Default value of 2000 milliseconds for
  12. * {@link ParticipantConnectionStatus.rtcMuteTimeout}.
  13. *
  14. * @type {number}
  15. */
  16. const DEFAULT_RTC_MUTE_TIMEOUT = 2000;
  17. /**
  18. * Class is responsible for emitting
  19. * JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED events.
  20. */
  21. export default class ParticipantConnectionStatus {
  22. /**
  23. * Creates new instance of <tt>ParticipantConnectionStatus</tt>.
  24. *
  25. * @constructor
  26. * @param {RTC} rtc the RTC service instance
  27. * @param {JitsiConference} conference parent conference instance
  28. * @param {number} rtcMuteTimeout (optional) custom value for
  29. * {@link ParticipantConnectionStatus.rtcMuteTimeout}.
  30. */
  31. constructor(rtc, conference, rtcMuteTimeout) {
  32. this.rtc = rtc;
  33. this.conference = conference;
  34. /**
  35. * A map of the "endpoint ID"(which corresponds to the resource part
  36. * of MUC JID(nickname)) to the timeout callback IDs scheduled using
  37. * window.setTimeout.
  38. * @type {Object.<string, number>}
  39. */
  40. this.trackTimers = {};
  41. /**
  42. * This map holds the endpoint connection status received from the JVB
  43. * (as it might be different than the one stored in JitsiParticipant).
  44. * Required for getting back in sync when remote video track is removed.
  45. * @type {Object.<string, boolean>}
  46. */
  47. this.connStatusFromJvb = { };
  48. /**
  49. * How long we're going to wait after the RTC video track muted event
  50. * for the corresponding signalling mute event, before the connection
  51. * interrupted is fired. The default value is
  52. * {@link DEFAULT_RTC_MUTE_TIMEOUT}.
  53. *
  54. * @type {number} amount of time in milliseconds
  55. */
  56. this.rtcMuteTimeout
  57. = typeof rtcMuteTimeout === 'number'
  58. ? rtcMuteTimeout : DEFAULT_RTC_MUTE_TIMEOUT;
  59. /**
  60. * This map holds a timestamp indicating when participant's video track
  61. * was RTC muted (it is assumed that each participant can have only 1
  62. * video track at a time). The purpose of storing the timestamp is to
  63. * avoid the transition to disconnected status in case of legitimate
  64. * video mute operation where the signalling video muted event can
  65. * arrive shortly after RTC muted event.
  66. *
  67. * The key is participant's ID which is the same as endpoint id in
  68. * the Colibri conference allocated on the JVB.
  69. *
  70. * The value is a timestamp measured in milliseconds obtained with
  71. * <tt>Date.now()</tt>.
  72. *
  73. * FIXME merge this logic with NO_DATA_FROM_SOURCE event
  74. * implemented in JitsiLocalTrack by extending the event to
  75. * the remote track and allowing to set different timeout for
  76. * local and remote tracks.
  77. *
  78. * @type {Object.<string, number>}
  79. */
  80. this.rtcMutedTimestamp = { };
  81. logger.info(`RtcMuteTimeout set to: ${this.rtcMuteTimeout}`);
  82. }
  83. /**
  84. * Initializes <tt>ParticipantConnectionStatus</tt> and bind required event
  85. * listeners.
  86. */
  87. init() {
  88. this._onEndpointConnStatusChanged
  89. = this.onEndpointConnStatusChanged.bind(this);
  90. this.rtc.addListener(
  91. RTCEvents.ENDPOINT_CONN_STATUS_CHANGED,
  92. this._onEndpointConnStatusChanged);
  93. // On some browsers MediaStreamTrack trigger "onmute"/"onunmute"
  94. // events for video type tracks when they stop receiving data which is
  95. // often a sign that remote user is having connectivity issues
  96. if (RTCBrowserType.isVideoMuteOnConnInterruptedSupported()) {
  97. this._onTrackRtcMuted = this.onTrackRtcMuted.bind(this);
  98. this.rtc.addListener(
  99. RTCEvents.REMOTE_TRACK_MUTE, this._onTrackRtcMuted);
  100. this._onTrackRtcUnmuted = this.onTrackRtcUnmuted.bind(this);
  101. this.rtc.addListener(
  102. RTCEvents.REMOTE_TRACK_UNMUTE, this._onTrackRtcUnmuted);
  103. // Track added/removed listeners are used to bind "mute"/"unmute"
  104. // event handlers
  105. this._onRemoteTrackAdded = this.onRemoteTrackAdded.bind(this);
  106. this.conference.on(
  107. JitsiConferenceEvents.TRACK_ADDED,
  108. this._onRemoteTrackAdded);
  109. this._onRemoteTrackRemoved = this.onRemoteTrackRemoved.bind(this);
  110. this.conference.on(
  111. JitsiConferenceEvents.TRACK_REMOVED,
  112. this._onRemoteTrackRemoved);
  113. // Listened which will be bound to JitsiRemoteTrack to listen for
  114. // signalling mute/unmute events.
  115. this._onSignallingMuteChanged
  116. = this.onSignallingMuteChanged.bind(this);
  117. }
  118. }
  119. /**
  120. * Removes all event listeners and disposes of all resources held by this
  121. * instance.
  122. */
  123. dispose() {
  124. this.rtc.removeListener(
  125. RTCEvents.ENDPOINT_CONN_STATUS_CHANGED,
  126. this._onEndpointConnStatusChanged);
  127. if (RTCBrowserType.isVideoMuteOnConnInterruptedSupported()) {
  128. this.rtc.removeListener(
  129. RTCEvents.REMOTE_TRACK_MUTE,
  130. this._onTrackRtcMuted);
  131. this.rtc.removeListener(
  132. RTCEvents.REMOTE_TRACK_UNMUTE,
  133. this._onTrackRtcUnmuted);
  134. this.conference.off(
  135. JitsiConferenceEvents.TRACK_ADDED,
  136. this._onRemoteTrackAdded);
  137. this.conference.off(
  138. JitsiConferenceEvents.TRACK_REMOVED,
  139. this._onRemoteTrackRemoved);
  140. }
  141. Object.keys(this.trackTimers).forEach(participantId => {
  142. this.clearTimeout(participantId);
  143. this.clearRtcMutedTimestamp(participantId);
  144. });
  145. // Clear RTC connection status cache
  146. this.connStatusFromJvb = {};
  147. }
  148. /**
  149. * Handles RTCEvents.ENDPOINT_CONN_STATUS_CHANGED triggered when we receive
  150. * notification over the data channel from the bridge about endpoint's
  151. * connection status update.
  152. * @param endpointId {string} the endpoint ID(MUC nickname/resource JID)
  153. * @param isActive {boolean} true if the connection is OK or false otherwise
  154. */
  155. onEndpointConnStatusChanged(endpointId, isActive) {
  156. logger.debug(
  157. `Detector RTCEvents.ENDPOINT_CONN_STATUS_CHANGED(${Date.now()}): ${
  158. endpointId}: ${isActive}`);
  159. // Filter out events for the local JID for now
  160. if (endpointId !== this.conference.myUserId()) {
  161. // Store the status received over the data channels
  162. this.connStatusFromJvb[endpointId] = isActive;
  163. this.figureOutConnectionStatus(endpointId);
  164. }
  165. }
  166. _changeConnectionStatus(participant, newStatus) {
  167. if (participant.isConnectionActive() !== newStatus) {
  168. const endpointId = participant.getId();
  169. participant._setIsConnectionActive(newStatus);
  170. logger.debug(
  171. `Emit endpoint conn status(${Date.now()}) ${endpointId}: ${
  172. newStatus}`);
  173. // Log the event on CallStats
  174. Statistics.sendLog(
  175. JSON.stringify({
  176. id: 'peer.conn.status',
  177. participant: endpointId,
  178. status: newStatus
  179. }));
  180. // and analytics
  181. Statistics.analytics.sendEvent('peer.conn.status',
  182. {label: newStatus});
  183. this.conference.eventEmitter.emit(
  184. JitsiConferenceEvents.PARTICIPANT_CONN_STATUS_CHANGED,
  185. endpointId, newStatus);
  186. }
  187. }
  188. /**
  189. * Reset the postponed "connection interrupted" event which was previously
  190. * scheduled as a timeout on RTC 'onmute' event.
  191. *
  192. * @param participantId the participant for which the "connection
  193. * interrupted" timeout was scheduled
  194. */
  195. clearTimeout(participantId) {
  196. if (this.trackTimers[participantId]) {
  197. window.clearTimeout(this.trackTimers[participantId]);
  198. this.trackTimers[participantId] = null;
  199. }
  200. }
  201. /**
  202. * Clears the timestamp of the RTC muted event for participant's video track
  203. * @param participantId the id of the conference participant which is
  204. * the same as the Colibri endpoint ID of the video channel allocated for
  205. * the user on the videobridge.
  206. */
  207. clearRtcMutedTimestamp(participantId) {
  208. this.rtcMutedTimestamp[participantId] = null;
  209. }
  210. /**
  211. * Bind signalling mute event listeners for video {JitsiRemoteTrack} when
  212. * a new one is added to the conference.
  213. *
  214. * @param {JitsiTrack} remoteTrack the {JitsiTrack} which is being added to
  215. * the conference.
  216. */
  217. onRemoteTrackAdded(remoteTrack) {
  218. if (!remoteTrack.isLocal()
  219. && remoteTrack.getType() === MediaType.VIDEO) {
  220. logger.debug(
  221. `Detector on remote track added for: ${
  222. remoteTrack.getParticipantId()}`);
  223. remoteTrack.on(
  224. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  225. this._onSignallingMuteChanged);
  226. }
  227. }
  228. /**
  229. * Removes all event listeners bound to the remote video track and clears
  230. * any related timeouts.
  231. *
  232. * @param {JitsiRemoteTrack} remoteTrack the remote track which is being
  233. * removed from the conference.
  234. */
  235. onRemoteTrackRemoved(remoteTrack) {
  236. if (!remoteTrack.isLocal()
  237. && remoteTrack.getType() === MediaType.VIDEO) {
  238. const endpointId = remoteTrack.getParticipantId();
  239. logger.debug(`Detector on remote track removed: ${endpointId}`);
  240. remoteTrack.off(
  241. JitsiTrackEvents.TRACK_MUTE_CHANGED,
  242. this._onSignallingMuteChanged);
  243. this.clearTimeout(endpointId);
  244. this.clearRtcMutedTimestamp(endpointId);
  245. this.figureOutConnectionStatus(endpointId);
  246. }
  247. }
  248. /**
  249. * Checks if given participant's video is considered frozen.
  250. * @param {JitsiParticipant} participant
  251. * @return {boolean} <tt>true</tt> if the video has frozen for given
  252. * participant or <tt>false</tt> when it's either not considered frozen
  253. * (yet) or if freeze detection is not supported by the current browser.
  254. *
  255. * FIXME merge this logic with NO_DATA_FROM_SOURCE event
  256. * implemented in JitsiLocalTrack by extending the event to
  257. * the remote track and allowing to set different timeout for
  258. * local and remote tracks.
  259. *
  260. */
  261. isVideoTrackFrozen(participant) {
  262. if (!RTCBrowserType.isVideoMuteOnConnInterruptedSupported()) {
  263. return false;
  264. }
  265. const hasAnyVideoRTCMuted = participant.hasAnyVideoTrackWebRTCMuted();
  266. const rtcMutedTimestamp
  267. = this.rtcMutedTimestamp[participant.getId()];
  268. return hasAnyVideoRTCMuted
  269. && typeof rtcMutedTimestamp === 'number'
  270. && (Date.now() - rtcMutedTimestamp) >= this.rtcMuteTimeout;
  271. }
  272. /**
  273. * Figures out (and updates) the current connectivity status for
  274. * the participant identified by the given id.
  275. *
  276. * @param {string} id the participant's id (MUC nickname or Colibri endpoint
  277. * ID).
  278. */
  279. figureOutConnectionStatus(id) {
  280. const participant = this.conference.getParticipantById(id);
  281. if (!participant) {
  282. // Probably the participant is no longer in the conference
  283. // (at the time of writing this code, participant is
  284. // detached from the conference and TRACK_REMOVED events are
  285. // fired),
  286. // so we don't care, but let's print the warning for
  287. // debugging purpose
  288. logger.warn(`figure out conn status - no participant for: ${id}`);
  289. return;
  290. }
  291. const isVideoMuted = participant.isVideoMuted();
  292. const isVideoTrackFrozen = this.isVideoTrackFrozen(participant);
  293. let isConnActiveByJvb = this.connStatusFromJvb[id];
  294. // If no status was received from the JVB it means that it's active
  295. // (the bridge does not send notification unless there is a problem).
  296. if (typeof isConnActiveByJvb !== 'boolean') {
  297. logger.debug('Assuming connection active by JVB - no notification');
  298. isConnActiveByJvb = true;
  299. }
  300. const isConnectionActive
  301. = isConnActiveByJvb && (isVideoMuted || !isVideoTrackFrozen);
  302. logger.debug(
  303. `Figure out conn status, is video muted: ${isVideoMuted
  304. } is active(jvb): ${isConnActiveByJvb
  305. } video track frozen: ${isVideoTrackFrozen
  306. } => ${isConnectionActive}`);
  307. this._changeConnectionStatus(participant, isConnectionActive);
  308. }
  309. /**
  310. * Handles RTC 'onmute' event for the video track.
  311. *
  312. * @param {JitsiRemoteTrack} track the video track for which 'onmute' event
  313. * will be processed.
  314. */
  315. onTrackRtcMuted(track) {
  316. const participantId = track.getParticipantId();
  317. const participant = this.conference.getParticipantById(participantId);
  318. logger.debug(`Detector track RTC muted: ${participantId}`);
  319. if (!participant) {
  320. logger.error(`No participant for id: ${participantId}`);
  321. return;
  322. }
  323. this.rtcMutedTimestamp[participantId] = Date.now();
  324. if (!participant.isVideoMuted()) {
  325. // If the user is not muted according to the signalling we'll give
  326. // it some time, before the connection interrupted event is
  327. // triggered.
  328. this.clearTimeout(participantId);
  329. this.trackTimers[participantId] = window.setTimeout(() => {
  330. logger.debug(`RTC mute timeout for: ${participantId}`);
  331. this.clearTimeout(participantId);
  332. this.figureOutConnectionStatus(participantId);
  333. }, this.rtcMuteTimeout);
  334. }
  335. }
  336. /**
  337. * Handles RTC 'onunmute' event for the video track.
  338. *
  339. * @param {JitsiRemoteTrack} track the video track for which 'onunmute'
  340. * event will be processed.
  341. */
  342. onTrackRtcUnmuted(track) {
  343. const participantId = track.getParticipantId();
  344. logger.debug(`Detector track RTC unmuted: ${participantId}`);
  345. this.clearTimeout(participantId);
  346. this.clearRtcMutedTimestamp(participantId);
  347. this.figureOutConnectionStatus(participantId);
  348. }
  349. /**
  350. * Here the signalling "mute"/"unmute" events are processed.
  351. *
  352. * @param {JitsiRemoteTrack} track the remote video track for which
  353. * the signalling mute/unmute event will be processed.
  354. */
  355. onSignallingMuteChanged(track) {
  356. const participantId = track.getParticipantId();
  357. logger.debug(
  358. `Detector on track signalling mute changed: ${participantId}`,
  359. track.isMuted());
  360. this.figureOutConnectionStatus(participantId);
  361. }
  362. }