modified lib-jitsi-meet dev repo
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.

AvgRTPStatsReporter.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* global __filename */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import * as ConnectionQualityEvents
  4. from '../../service/connectivity/ConnectionQualityEvents';
  5. import * as ConferenceEvents from '../../JitsiConferenceEvents';
  6. import RTCBrowserType from '../RTC/RTCBrowserType';
  7. import Statistics from './statistics';
  8. const logger = getLogger(__filename);
  9. /**
  10. * This will calculate an average for one, named stat and submit it to
  11. * the analytics module when requested. It automatically counts the samples.
  12. */
  13. class AverageStatReport {
  14. /**
  15. * Creates new <tt>AverageStatReport</tt> for given name.
  16. * @param {string} name that's the name of the event that will be reported
  17. * to the analytics module.
  18. */
  19. constructor(name) {
  20. this.name = name;
  21. this.count = 0;
  22. this.sum = 0;
  23. }
  24. /**
  25. * Adds the next value that will be included in the average when
  26. * {@link calculate} is called.
  27. * @param {number} nextValue
  28. */
  29. addNext(nextValue) {
  30. if (typeof nextValue !== 'number') {
  31. logger.error(
  32. `${this.name} - invalid value for idx: ${this.count}`,
  33. nextValue);
  34. return;
  35. }
  36. this.sum += nextValue;
  37. this.count += 1;
  38. }
  39. /**
  40. * Calculates an average for the samples collected using {@link addNext}.
  41. * @return {number|NaN} an average of all collected samples or <tt>NaN</tt>
  42. * if no samples were collected.
  43. */
  44. calculate() {
  45. return this.sum / this.count;
  46. }
  47. /**
  48. * Calculates an average and submit the report to the analytics module.
  49. * @param {boolean} isP2P indicates if the report is to be submitted for
  50. * the P2P connection (when conference is currently in the P2P mode). This
  51. * will add 'p2p.' prefix to the name of the event. All averages should be
  52. * cleared when the conference switches, between P2P and JVB modes.
  53. */
  54. report(isP2P) {
  55. Statistics.analytics.sendEvent(
  56. `${isP2P ? 'p2p.' : ''}${this.name}`,
  57. { value: this.calculate() });
  58. }
  59. /**
  60. * Clears all memory of any samples collected, so that new average can be
  61. * calculated using this instance.
  62. */
  63. reset() {
  64. this.sum = 0;
  65. this.count = 0;
  66. }
  67. }
  68. /**
  69. * Reports average RTP statistics values (arithmetic mean) to the analytics
  70. * module for things like bit rate, bandwidth, packet loss etc. It keeps track
  71. * of the P2P vs JVB conference modes and submits the values under different
  72. * namespaces (the events for P2P mode have 'p2p.' prefix). Every switch between
  73. * P2P mode resets the data collected so far and averages are calculated from
  74. * scratch.
  75. */
  76. export default class AvgRTPStatsReporter {
  77. /**
  78. * Creates new instance of <tt>AvgRTPStatsReporter</tt>
  79. * @param {JitsiConference} conference
  80. * @param {number} n the number of samples, before arithmetic mean is to be
  81. * calculated and values submitted to the analytics module.
  82. */
  83. constructor(conference, n) {
  84. /**
  85. * How many {@link ConnectionQualityEvents.LOCAL_STATS_UPDATED} samples
  86. * are to be included in arithmetic mean calculation.
  87. * @type {number}
  88. * @private
  89. */
  90. this._n = n;
  91. if (n > 0) {
  92. logger.info(`Avg RTP stats will be calculated every ${n} samples`);
  93. } else {
  94. logger.info('Avg RTP stats reports are disabled.');
  95. // Do not initialize
  96. return;
  97. }
  98. /**
  99. * The current sample index. Starts from 0 and goes up to {@link _n})
  100. * when analytics report will be submitted.
  101. * @type {number}
  102. * @private
  103. */
  104. this._sampleIdx = 0;
  105. /**
  106. * The conference for which stats will be collected and reported.
  107. * @type {JitsiConference}
  108. * @private
  109. */
  110. this._conference = conference;
  111. /**
  112. * Average upload bitrate
  113. * @type {AverageStatReport}
  114. * @private
  115. */
  116. this._avgBitrateUp = new AverageStatReport('stat.avg.bitrate.upload');
  117. /**
  118. * Average download bitrate
  119. * @type {AverageStatReport}
  120. * @private
  121. */
  122. this._avgBitrateDown
  123. = new AverageStatReport('stat.avg.bitrate.download');
  124. /**
  125. * Average upload bandwidth
  126. * @type {AverageStatReport}
  127. * @private
  128. */
  129. this._avgBandwidthUp
  130. = new AverageStatReport('stat.avg.bandwidth.upload');
  131. /**
  132. * Average download bandwidth
  133. * @type {AverageStatReport}
  134. * @private
  135. */
  136. this._avgBandwidthDown
  137. = new AverageStatReport('stat.avg.bandwidth.download');
  138. /**
  139. * Average total packet loss
  140. * @type {AverageStatReport}
  141. * @private
  142. */
  143. this._avgPacketLossTotal
  144. = new AverageStatReport('stat.avg.packetloss.total');
  145. /**
  146. * Average upload packet loss
  147. * @type {AverageStatReport}
  148. * @private
  149. */
  150. this._avgPacketLossUp
  151. = new AverageStatReport('stat.avg.packetloss.upload');
  152. /**
  153. * Average download packet loss
  154. * @type {AverageStatReport}
  155. * @private
  156. */
  157. this._avgPacketLossDown
  158. = new AverageStatReport('stat.avg.packetloss.download');
  159. /**
  160. * Average FPS for remote videos
  161. * @type {AverageStatReport}
  162. * @private
  163. */
  164. this._avgRemoteFPS = new AverageStatReport('stat.avg.framerate.remote');
  165. /**
  166. * Average FPS for local video
  167. * @type {AverageStatReport}
  168. * @private
  169. */
  170. this._avgLocalFPS = new AverageStatReport('stat.avg.framerate.local');
  171. /**
  172. * Average connection quality as defined by
  173. * the {@link ConnectionQuality} module.
  174. * @type {AverageStatReport}
  175. * @private
  176. */
  177. this._avgCQ = new AverageStatReport('stat.avg.cq');
  178. this._onLocalStatsUpdated = data => this._calculateAvgStats(data);
  179. conference.on(
  180. ConnectionQualityEvents.LOCAL_STATS_UPDATED,
  181. this._onLocalStatsUpdated);
  182. this._onP2PStatusChanged = () => {
  183. logger.debug('Resetting average stats calculation');
  184. this._resetAvgStats();
  185. };
  186. conference.on(
  187. ConferenceEvents.P2P_STATUS,
  188. this._onP2PStatusChanged);
  189. }
  190. /**
  191. * Processes next batch of stats reported on
  192. * {@link ConnectionQualityEvents.LOCAL_STATS_UPDATED}.
  193. * @param {go figure} data
  194. * @private
  195. */
  196. _calculateAvgStats(data) {
  197. const isP2P = this._conference.isP2PActive();
  198. const peerCount = this._conference.getParticipants().length;
  199. if (!isP2P && peerCount < 1) {
  200. // There's no point in collecting stats for a JVB conference of 1.
  201. // That happens for short period of time after everyone leaves
  202. // the room, until Jicofo terminates the session.
  203. return;
  204. }
  205. /* Uncomment to figure out stats structure
  206. for (const key in data) {
  207. if (data.hasOwnProperty(key)) {
  208. logger.info(`local stat ${key}: `, data[key]);
  209. }
  210. } */
  211. if (!data) {
  212. logger.error('No stats');
  213. return;
  214. }
  215. const bitrate = data.bitrate;
  216. const bandwidth = data.bandwidth;
  217. const packetLoss = data.packetLoss;
  218. const frameRate = data.framerate;
  219. if (!bitrate) {
  220. logger.error('No "bitrate"');
  221. return;
  222. } else if (!bandwidth) {
  223. logger.error('No "bandwidth"');
  224. return;
  225. } else if (!packetLoss) {
  226. logger.error('No "packetloss"');
  227. return;
  228. } else if (!frameRate) {
  229. logger.error('No "framerate"');
  230. return;
  231. }
  232. this._avgBitrateUp.addNext(bitrate.upload);
  233. this._avgBitrateDown.addNext(bitrate.download);
  234. if (RTCBrowserType.supportsBandwidthStatistics()) {
  235. this._avgBandwidthUp.addNext(bandwidth.upload);
  236. this._avgBandwidthDown.addNext(bandwidth.download);
  237. }
  238. this._avgPacketLossUp.addNext(packetLoss.upload);
  239. this._avgPacketLossDown.addNext(packetLoss.download);
  240. this._avgPacketLossTotal.addNext(packetLoss.total);
  241. this._avgCQ.addNext(data.connectionQuality);
  242. if (frameRate) {
  243. this._avgRemoteFPS.addNext(
  244. this._calculateAvgVideoFps(frameRate, false /* remote */));
  245. this._avgLocalFPS.addNext(
  246. this._calculateAvgVideoFps(frameRate, true /* local */));
  247. }
  248. this._sampleIdx += 1;
  249. if (this._sampleIdx >= this._n) {
  250. this._avgBitrateUp.report(isP2P);
  251. this._avgBitrateDown.report(isP2P);
  252. if (RTCBrowserType.supportsBandwidthStatistics()) {
  253. this._avgBandwidthUp.report(isP2P);
  254. this._avgBandwidthDown.report(isP2P);
  255. }
  256. this._avgPacketLossUp.report(isP2P);
  257. this._avgPacketLossDown.report(isP2P);
  258. this._avgPacketLossTotal.report(isP2P);
  259. this._avgRemoteFPS.report(isP2P);
  260. this._avgLocalFPS.report(isP2P);
  261. this._avgCQ.report(isP2P);
  262. this._resetAvgStats();
  263. }
  264. }
  265. /**
  266. * Calculates average FPS for the report
  267. * @param {go figure} frameRate
  268. * @param {boolean} isLocal if the average is to be calculated for the local
  269. * video or <tt>false</tt> if for remote videos.
  270. * @return {number|NaN} average FPS or <tt>NaN</tt> if there are no samples.
  271. * @private
  272. */
  273. _calculateAvgVideoFps(frameRate, isLocal) {
  274. let peerCount = 0;
  275. let subFrameAvg = 0;
  276. const myID = this._conference.myUserId();
  277. for (const peerID of Object.keys(frameRate)) {
  278. if (isLocal ? peerID === myID : peerID !== myID) {
  279. const videos = frameRate[peerID];
  280. const ssrcs = Object.keys(videos);
  281. if (ssrcs.length) {
  282. let peerAvg = 0;
  283. for (const ssrc of ssrcs) {
  284. peerAvg += parseInt(videos[ssrc], 10);
  285. }
  286. peerAvg /= ssrcs.length;
  287. subFrameAvg += peerAvg;
  288. peerCount += 1;
  289. }
  290. }
  291. }
  292. return subFrameAvg / peerCount;
  293. }
  294. /**
  295. * Reset cache of all averages and {@link _sampleIdx}.
  296. * @private
  297. */
  298. _resetAvgStats() {
  299. this._avgBitrateUp.reset();
  300. this._avgBitrateDown.reset();
  301. this._avgBandwidthUp.reset();
  302. this._avgBandwidthDown.reset();
  303. this._avgPacketLossUp.reset();
  304. this._avgPacketLossDown.reset();
  305. this._avgRemoteFPS.reset();
  306. this._avgLocalFPS.reset();
  307. this._avgCQ.reset();
  308. this._sampleIdx = 0;
  309. }
  310. /**
  311. * Unregisters all event listeners and stops working.
  312. */
  313. dispose() {
  314. if (this._onP2PStatusChanged) {
  315. this._conference.off(
  316. ConferenceEvents.P2P_STATUS,
  317. this._onP2PStatusChanged);
  318. }
  319. if (this._onLocalStatsUpdated) {
  320. this._conference.off(
  321. ConnectionQualityEvents.LOCAL_STATS_UPDATED,
  322. this._onLocalStatsUpdated);
  323. }
  324. }
  325. }