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 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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 * as MediaType from '../../service/RTC/MediaType';
  7. import RTCBrowserType from '../RTC/RTCBrowserType';
  8. import Statistics from './statistics';
  9. import * as VideoType from '../../service/RTC/VideoType';
  10. /**
  11. * All avg RTP stats are currently reported under 1 event name, but under
  12. * different keys. This constant stores the name of this event.
  13. * Example structure of "avg.rtp.stats" analytics event:
  14. *
  15. * {
  16. * "stat_avg_rtt": {
  17. * value: 200,
  18. * samples: [ 100, 200, 300 ]
  19. * },
  20. * "stat_avg_packetloss_total": {
  21. * value: 10,
  22. * samples: [ 5, 10, 15]
  23. * },
  24. * "p2p_stat_avg_packetloss_total": {
  25. * value: 15,
  26. * samples: [ 10, 15, 20]
  27. * }
  28. * }
  29. *
  30. * Note that the samples array is currently emitted for debug purposes only and
  31. * can be removed anytime soon from the structure.
  32. *
  33. * Also not all values are always present in "avg.rtp.stats", some of the values
  34. * are obtained and calculated as part of different process/event pipe. For
  35. * example {@link ConnectionAvgStats} instances are doing the reports for each
  36. * {@link TraceablePeerConnection} and work independently from the main stats
  37. * pipe.
  38. *
  39. * @type {string}
  40. */
  41. const AVG_RTP_STATS_EVENT = 'avg.rtp.stats';
  42. const logger = getLogger(__filename);
  43. /**
  44. * This will calculate an average for one, named stat and submit it to
  45. * the analytics module when requested. It automatically counts the samples.
  46. */
  47. class AverageStatReport {
  48. /**
  49. * Creates new <tt>AverageStatReport</tt> for given name.
  50. * @param {string} name that's the name of the event that will be reported
  51. * to the analytics module.
  52. */
  53. constructor(name) {
  54. this.name = name;
  55. this.count = 0;
  56. this.sum = 0;
  57. this.samples = [];
  58. }
  59. /**
  60. * Adds the next value that will be included in the average when
  61. * {@link calculate} is called.
  62. * @param {number} nextValue
  63. */
  64. addNext(nextValue) {
  65. if (typeof nextValue !== 'number') {
  66. logger.error(
  67. `${this.name} - invalid value for idx: ${this.count}`,
  68. nextValue);
  69. } else if (!isNaN(nextValue)) {
  70. this.sum += nextValue;
  71. this.samples.push(nextValue);
  72. this.count += 1;
  73. }
  74. }
  75. /**
  76. * Calculates an average for the samples collected using {@link addNext}.
  77. * @return {number|NaN} an average of all collected samples or <tt>NaN</tt>
  78. * if no samples were collected.
  79. */
  80. calculate() {
  81. return this.sum / this.count;
  82. }
  83. /**
  84. * Appends the report to the analytics "data" object. The object will be
  85. * added under {@link this.name} key or "p2p_" + {@link this.name} if
  86. * <tt>isP2P</tt> is <tt>true</tt>.
  87. * @param {Object} report the analytics "data" object
  88. * @param {boolean} isP2P <tt>true</tt> if the stats is being report for
  89. * P2P connection or <tt>false</tt> for the JVB connection.
  90. */
  91. appendReport(report, isP2P) {
  92. report[`${isP2P ? 'p2p_' : ''}${this.name}`] = {
  93. value: this.calculate(),
  94. samples: this.samples
  95. };
  96. }
  97. /**
  98. * Clears all memory of any samples collected, so that new average can be
  99. * calculated using this instance.
  100. */
  101. reset() {
  102. this.samples = [];
  103. this.sum = 0;
  104. this.count = 0;
  105. }
  106. }
  107. /**
  108. * Class gathers the stats that are calculated and reported for a
  109. * {@link TraceablePeerConnection} even if it's not currently active. For
  110. * example we want to monitor RTT for the JVB connection while in P2P mode.
  111. */
  112. class ConnectionAvgStats {
  113. /**
  114. * Creates new <tt>ConnectionAvgStats</tt>
  115. * @param {JitsiConference} conference
  116. * @param {boolean} isP2P
  117. * @param {number} n the number of samples, before arithmetic mean is to be
  118. * calculated and values submitted to the analytics module.
  119. */
  120. constructor(conference, isP2P, n) {
  121. /**
  122. * Is this instance for JVB or P2P connection ?
  123. * @type {boolean}
  124. */
  125. this.isP2P = isP2P;
  126. /**
  127. * How many samples are to be included in arithmetic mean calculation.
  128. * @type {number}
  129. * @private
  130. */
  131. this._n = n;
  132. /**
  133. * The current sample index. Starts from 0 and goes up to {@link _n})
  134. * when analytics report will be submitted.
  135. * @type {number}
  136. * @private
  137. */
  138. this._sampleIdx = 0;
  139. /**
  140. * Average round trip time reported by the ICE candidate pair.
  141. * @type {AverageStatReport}
  142. */
  143. this._avgRTT = new AverageStatReport('stat_avg_rtt');
  144. /**
  145. * Map stores average RTT to the JVB reported by remote participants.
  146. * Mapped per participant id {@link JitsiParticipant.getId}.
  147. *
  148. * This is used only when {@link ConnectionAvgStats.isP2P} equals to
  149. * <tt>false</tt>.
  150. *
  151. * @type {Map<string,AverageStatReport>}
  152. * @private
  153. */
  154. this._avgRemoteRTTMap = new Map();
  155. /**
  156. * The conference for which stats will be collected and reported.
  157. * @type {JitsiConference}
  158. * @private
  159. */
  160. this._conference = conference;
  161. this._onConnectionStats = (tpc, stats) => {
  162. if (this.isP2P === tpc.isP2P) {
  163. this._calculateAvgStats(stats);
  164. }
  165. };
  166. conference.statistics.addConnectionStatsListener(
  167. this._onConnectionStats);
  168. if (!this.isP2P) {
  169. this._onUserLeft = id => this._avgRemoteRTTMap.delete(id);
  170. conference.on(ConferenceEvents.USER_LEFT, this._onUserLeft);
  171. this._onRemoteStatsUpdated
  172. = (id, data) => this._processRemoteStats(id, data);
  173. conference.on(
  174. ConnectionQualityEvents.REMOTE_STATS_UPDATED,
  175. this._onRemoteStatsUpdated);
  176. }
  177. }
  178. /**
  179. * Processes next batch of stats.
  180. * @param {go figure} data
  181. * @private
  182. */
  183. _calculateAvgStats(data) {
  184. if (!data) {
  185. logger.error('No stats');
  186. return;
  187. }
  188. if (RTCBrowserType.supportsRTTStatistics()) {
  189. if (data.transport && data.transport.length) {
  190. this._avgRTT.addNext(data.transport[0].rtt);
  191. }
  192. }
  193. this._sampleIdx += 1;
  194. if (this._sampleIdx >= this._n) {
  195. if (RTCBrowserType.supportsRTTStatistics()) {
  196. const batchReport = { };
  197. this._avgRTT.appendReport(batchReport, this.isP2P);
  198. // Report end to end RTT only for JVB
  199. if (!this.isP2P) {
  200. const avgRemoteRTT = this._calculateAvgRemoteRTT();
  201. const avgLocalRTT = this._avgRTT.calculate();
  202. if (!isNaN(avgLocalRTT) && !isNaN(avgRemoteRTT)) {
  203. // eslint-disable-next-line camelcase
  204. batchReport.stat_avg_end2endrtt
  205. = { value: avgLocalRTT + avgRemoteRTT };
  206. }
  207. }
  208. Statistics.analytics.sendEvent(
  209. AVG_RTP_STATS_EVENT, batchReport);
  210. }
  211. this._resetAvgStats();
  212. }
  213. }
  214. /**
  215. * Calculates arithmetic mean of all RTTs towards the JVB reported by
  216. * participants.
  217. * @return {number|NaN} NaN if not available (not enough data)
  218. * @private
  219. */
  220. _calculateAvgRemoteRTT() {
  221. let count = 0, sum = 0;
  222. // FIXME should we ignore RTT for participant
  223. // who "is having connectivity issues" ?
  224. for (const remoteAvg of this._avgRemoteRTTMap.values()) {
  225. const avg = remoteAvg.calculate();
  226. if (!isNaN(avg)) {
  227. sum += avg;
  228. count += 1;
  229. remoteAvg.reset();
  230. }
  231. }
  232. return sum / count;
  233. }
  234. /**
  235. * Processes {@link ConnectionQualityEvents.REMOTE_STATS_UPDATED} to analyse
  236. * RTT towards the JVB reported by each participant.
  237. * @param {string} id {@link JitsiParticipant.getId}
  238. * @param {go figure in ConnectionQuality.js} data
  239. * @private
  240. */
  241. _processRemoteStats(id, data) {
  242. const validData = typeof data.jvbRTT === 'number';
  243. let rttAvg = this._avgRemoteRTTMap.get(id);
  244. if (!rttAvg && validData) {
  245. rttAvg = new AverageStatReport(`${id}_stat_rtt`);
  246. this._avgRemoteRTTMap.set(id, rttAvg);
  247. }
  248. if (validData) {
  249. rttAvg.addNext(data.jvbRTT);
  250. } else if (rttAvg) {
  251. this._avgRemoteRTTMap.delete(id);
  252. }
  253. }
  254. /**
  255. * Reset cache of all averages and {@link _sampleIdx}.
  256. * @private
  257. */
  258. _resetAvgStats() {
  259. this._avgRTT.reset();
  260. if (this._avgRemoteRTTMap) {
  261. this._avgRemoteRTTMap.clear();
  262. }
  263. this._sampleIdx = 0;
  264. }
  265. /**
  266. *
  267. */
  268. dispose() {
  269. this._conference.statistics.removeConnectionStatsListener(
  270. this._onConnectionStats);
  271. if (!this.isP2P) {
  272. this._conference.off(
  273. ConnectionQualityEvents.REMOTE_STATS_UPDATED,
  274. this._onRemoteStatsUpdated);
  275. this._conference.off(
  276. ConferenceEvents.USER_LEFT,
  277. this._onUserLeft);
  278. }
  279. }
  280. }
  281. /**
  282. * Reports average RTP statistics values (arithmetic mean) to the analytics
  283. * module for things like bit rate, bandwidth, packet loss etc. It keeps track
  284. * of the P2P vs JVB conference modes and submits the values under different
  285. * namespaces (the events for P2P mode have 'p2p.' prefix). Every switch between
  286. * P2P mode resets the data collected so far and averages are calculated from
  287. * scratch.
  288. */
  289. export default class AvgRTPStatsReporter {
  290. /**
  291. * Creates new instance of <tt>AvgRTPStatsReporter</tt>
  292. * @param {JitsiConference} conference
  293. * @param {number} n the number of samples, before arithmetic mean is to be
  294. * calculated and values submitted to the analytics module.
  295. */
  296. constructor(conference, n) {
  297. /**
  298. * How many {@link ConnectionQualityEvents.LOCAL_STATS_UPDATED} samples
  299. * are to be included in arithmetic mean calculation.
  300. * @type {number}
  301. * @private
  302. */
  303. this._n = n;
  304. if (n > 0) {
  305. logger.info(`Avg RTP stats will be calculated every ${n} samples`);
  306. } else {
  307. logger.info('Avg RTP stats reports are disabled.');
  308. // Do not initialize
  309. return;
  310. }
  311. /**
  312. * The current sample index. Starts from 0 and goes up to {@link _n})
  313. * when analytics report will be submitted.
  314. * @type {number}
  315. * @private
  316. */
  317. this._sampleIdx = 0;
  318. /**
  319. * The conference for which stats will be collected and reported.
  320. * @type {JitsiConference}
  321. * @private
  322. */
  323. this._conference = conference;
  324. /**
  325. * Average audio upload bitrate
  326. * @type {AverageStatReport}
  327. * @private
  328. */
  329. this._avgAudioBitrateUp
  330. = new AverageStatReport('stat_avg_bitrate_audio_upload');
  331. /**
  332. * Average audio download bitrate
  333. * @type {AverageStatReport}
  334. * @private
  335. */
  336. this._avgAudioBitrateDown
  337. = new AverageStatReport('stat_avg_bitrate_audio_download');
  338. /**
  339. * Average video upload bitrate
  340. * @type {AverageStatReport}
  341. * @private
  342. */
  343. this._avgVideoBitrateUp
  344. = new AverageStatReport('stat_avg_bitrate_video_upload');
  345. /**
  346. * Average video download bitrate
  347. * @type {AverageStatReport}
  348. * @private
  349. */
  350. this._avgVideoBitrateDown
  351. = new AverageStatReport('stat_avg_bitrate_video_download');
  352. /**
  353. * Average upload bandwidth
  354. * @type {AverageStatReport}
  355. * @private
  356. */
  357. this._avgBandwidthUp
  358. = new AverageStatReport('stat_avg_bandwidth_upload');
  359. /**
  360. * Average download bandwidth
  361. * @type {AverageStatReport}
  362. * @private
  363. */
  364. this._avgBandwidthDown
  365. = new AverageStatReport('stat_avg_bandwidth_download');
  366. /**
  367. * Average total packet loss
  368. * @type {AverageStatReport}
  369. * @private
  370. */
  371. this._avgPacketLossTotal
  372. = new AverageStatReport('stat_avg_packetloss_total');
  373. /**
  374. * Average upload packet loss
  375. * @type {AverageStatReport}
  376. * @private
  377. */
  378. this._avgPacketLossUp
  379. = new AverageStatReport('stat_avg_packetloss_upload');
  380. /**
  381. * Average download packet loss
  382. * @type {AverageStatReport}
  383. * @private
  384. */
  385. this._avgPacketLossDown
  386. = new AverageStatReport('stat_avg_packetloss_download');
  387. /**
  388. * Average FPS for remote videos
  389. * @type {AverageStatReport}
  390. * @private
  391. */
  392. this._avgRemoteFPS = new AverageStatReport('stat_avg_framerate_remote');
  393. /**
  394. * Average FPS for remote screen streaming videos (reported only if not
  395. * a <tt>NaN</tt>).
  396. * @type {AverageStatReport}
  397. * @private
  398. */
  399. this._avgRemoteScreenFPS
  400. = new AverageStatReport('stat_avg_framerate_screen_remote');
  401. /**
  402. * Average FPS for local video (camera)
  403. * @type {AverageStatReport}
  404. * @private
  405. */
  406. this._avgLocalFPS = new AverageStatReport('stat_avg_framerate_local');
  407. /**
  408. * Average FPS for local screen streaming video (reported only if not
  409. * a <tt>NaN</tt>).
  410. * @type {AverageStatReport}
  411. * @private
  412. */
  413. this._avgLocalScreenFPS
  414. = new AverageStatReport('stat_avg_framerate_screen_local');
  415. /**
  416. * Average connection quality as defined by
  417. * the {@link ConnectionQuality} module.
  418. * @type {AverageStatReport}
  419. * @private
  420. */
  421. this._avgCQ = new AverageStatReport('stat_avg_cq');
  422. this._onLocalStatsUpdated = data => this._calculateAvgStats(data);
  423. conference.on(
  424. ConnectionQualityEvents.LOCAL_STATS_UPDATED,
  425. this._onLocalStatsUpdated);
  426. this._onP2PStatusChanged = () => {
  427. logger.debug('Resetting average stats calculation');
  428. this._resetAvgStats();
  429. this.jvbStatsMonitor._resetAvgStats();
  430. this.p2pStatsMonitor._resetAvgStats();
  431. };
  432. conference.on(
  433. ConferenceEvents.P2P_STATUS,
  434. this._onP2PStatusChanged);
  435. this.jvbStatsMonitor
  436. = new ConnectionAvgStats(conference, false /* JVB */, n);
  437. this.p2pStatsMonitor
  438. = new ConnectionAvgStats(conference, true /* P2P */, n);
  439. }
  440. /**
  441. * Processes next batch of stats reported on
  442. * {@link ConnectionQualityEvents.LOCAL_STATS_UPDATED}.
  443. * @param {go figure} data
  444. * @private
  445. */
  446. _calculateAvgStats(data) {
  447. const isP2P = this._conference.isP2PActive();
  448. const peerCount = this._conference.getParticipants().length;
  449. if (!isP2P && peerCount < 1) {
  450. // There's no point in collecting stats for a JVB conference of 1.
  451. // That happens for short period of time after everyone leaves
  452. // the room, until Jicofo terminates the session.
  453. return;
  454. }
  455. /* Uncomment to figure out stats structure
  456. for (const key in data) {
  457. if (data.hasOwnProperty(key)) {
  458. logger.info(`local stat ${key}: `, data[key]);
  459. }
  460. } */
  461. if (!data) {
  462. logger.error('No stats');
  463. return;
  464. }
  465. const bitrate = data.bitrate;
  466. const bandwidth = data.bandwidth;
  467. const packetLoss = data.packetLoss;
  468. const frameRate = data.framerate;
  469. if (!bitrate) {
  470. logger.error('No "bitrate"');
  471. return;
  472. } else if (!bandwidth) {
  473. logger.error('No "bandwidth"');
  474. return;
  475. } else if (!packetLoss) {
  476. logger.error('No "packetloss"');
  477. return;
  478. } else if (!frameRate) {
  479. logger.error('No "framerate"');
  480. return;
  481. }
  482. this._avgAudioBitrateUp.addNext(bitrate.audio.upload);
  483. this._avgAudioBitrateDown.addNext(bitrate.audio.download);
  484. this._avgVideoBitrateUp.addNext(bitrate.video.upload);
  485. this._avgVideoBitrateDown.addNext(bitrate.video.download);
  486. if (RTCBrowserType.supportsBandwidthStatistics()) {
  487. this._avgBandwidthUp.addNext(bandwidth.upload);
  488. this._avgBandwidthDown.addNext(bandwidth.download);
  489. }
  490. this._avgPacketLossUp.addNext(packetLoss.upload);
  491. this._avgPacketLossDown.addNext(packetLoss.download);
  492. this._avgPacketLossTotal.addNext(packetLoss.total);
  493. this._avgCQ.addNext(data.connectionQuality);
  494. if (frameRate) {
  495. this._avgRemoteFPS.addNext(
  496. this._calculateAvgVideoFps(
  497. frameRate, false /* remote */, VideoType.CAMERA));
  498. this._avgRemoteScreenFPS.addNext(
  499. this._calculateAvgVideoFps(
  500. frameRate, false /* remote */, VideoType.DESKTOP));
  501. this._avgLocalFPS.addNext(
  502. this._calculateAvgVideoFps(
  503. frameRate, true /* local */, VideoType.CAMERA));
  504. this._avgLocalScreenFPS.addNext(
  505. this._calculateAvgVideoFps(
  506. frameRate, true /* local */, VideoType.DESKTOP));
  507. }
  508. this._sampleIdx += 1;
  509. if (this._sampleIdx >= this._n) {
  510. const batchReport = { };
  511. this._avgAudioBitrateUp.appendReport(batchReport, isP2P);
  512. this._avgAudioBitrateDown.appendReport(batchReport, isP2P);
  513. this._avgVideoBitrateUp.appendReport(batchReport, isP2P);
  514. this._avgVideoBitrateDown.appendReport(batchReport, isP2P);
  515. if (RTCBrowserType.supportsBandwidthStatistics()) {
  516. this._avgBandwidthUp.appendReport(batchReport, isP2P);
  517. this._avgBandwidthDown.appendReport(batchReport, isP2P);
  518. }
  519. this._avgPacketLossUp.appendReport(batchReport, isP2P);
  520. this._avgPacketLossDown.appendReport(batchReport, isP2P);
  521. this._avgPacketLossTotal.appendReport(batchReport, isP2P);
  522. this._avgRemoteFPS.appendReport(batchReport, isP2P);
  523. if (!isNaN(this._avgRemoteScreenFPS.calculate())) {
  524. this._avgRemoteScreenFPS.appendReport(batchReport, isP2P);
  525. }
  526. this._avgLocalFPS.appendReport(batchReport, isP2P);
  527. if (!isNaN(this._avgLocalScreenFPS.calculate())) {
  528. this._avgLocalScreenFPS.appendReport(batchReport, isP2P);
  529. }
  530. this._avgCQ.appendReport(batchReport, isP2P);
  531. Statistics.analytics.sendEvent(AVG_RTP_STATS_EVENT, batchReport);
  532. this._resetAvgStats();
  533. }
  534. }
  535. /**
  536. * Calculates average FPS for the report
  537. * @param {go figure} frameRate
  538. * @param {boolean} isLocal if the average is to be calculated for the local
  539. * video or <tt>false</tt> if for remote videos.
  540. * @param {VideoType} videoType
  541. * @return {number|NaN} average FPS or <tt>NaN</tt> if there are no samples.
  542. * @private
  543. */
  544. _calculateAvgVideoFps(frameRate, isLocal, videoType) {
  545. let peerFpsSum = 0;
  546. let peerCount = 0;
  547. const myID = this._conference.myUserId();
  548. for (const peerID of Object.keys(frameRate)) {
  549. if (isLocal ? peerID === myID : peerID !== myID) {
  550. const participant
  551. = isLocal
  552. ? null : this._conference.getParticipantById(peerID);
  553. const videosFps = frameRate[peerID];
  554. // Do not continue without participant for non local peerID
  555. if ((isLocal || participant) && videosFps) {
  556. const peerAvgFPS
  557. = this._calculatePeerAvgVideoFps(
  558. videosFps, participant, videoType);
  559. if (!isNaN(peerAvgFPS)) {
  560. peerFpsSum += peerAvgFPS;
  561. peerCount += 1;
  562. }
  563. }
  564. }
  565. }
  566. return peerFpsSum / peerCount;
  567. }
  568. /**
  569. * Calculate average FPS for either remote or local participant
  570. * @param {object} videos maps FPS per video SSRC
  571. * @param {JitsiParticipant|null} participant remote participant or
  572. * <tt>null</tt> for local FPS calculation.
  573. * @param {VideoType} videoType the type of the video for which an average
  574. * will be calculated.
  575. * @return {number|NaN} average FPS of all participant's videos or
  576. * <tt>NaN</tt> if currently not available
  577. * @private
  578. */
  579. _calculatePeerAvgVideoFps(videos, participant, videoType) {
  580. let ssrcs = Object.keys(videos).map(ssrc => Number(ssrc));
  581. let videoTracks = null;
  582. // NOTE that this method is supposed to be called for the stats
  583. // received from the current peerconnection.
  584. const tpc = this._conference.getActivePeerConnection();
  585. if (participant) {
  586. videoTracks = participant.getTracksByMediaType(MediaType.VIDEO);
  587. if (videoTracks) {
  588. ssrcs
  589. = ssrcs.filter(
  590. ssrc => videoTracks.find(
  591. track => !track.isMuted()
  592. && track.getSSRC() === ssrc
  593. && track.videoType === videoType));
  594. }
  595. } else {
  596. videoTracks = this._conference.getLocalTracks(MediaType.VIDEO);
  597. ssrcs
  598. = ssrcs.filter(
  599. ssrc => videoTracks.find(
  600. track => !track.isMuted()
  601. && tpc.getLocalSSRC(track) === ssrc
  602. && track.videoType === videoType));
  603. }
  604. let peerFpsSum = 0;
  605. let peerSsrcCount = 0;
  606. for (const ssrc of ssrcs) {
  607. const peerSsrcFps = Number(videos[ssrc]);
  608. // FPS is reported as 0 for users with no video
  609. if (!isNaN(peerSsrcFps) && peerSsrcFps > 0) {
  610. peerFpsSum += peerSsrcFps;
  611. peerSsrcCount += 1;
  612. }
  613. }
  614. return peerFpsSum / peerSsrcCount;
  615. }
  616. /**
  617. * Reset cache of all averages and {@link _sampleIdx}.
  618. * @private
  619. */
  620. _resetAvgStats() {
  621. this._avgAudioBitrateUp.reset();
  622. this._avgAudioBitrateDown.reset();
  623. this._avgVideoBitrateUp.reset();
  624. this._avgVideoBitrateDown.reset();
  625. this._avgBandwidthUp.reset();
  626. this._avgBandwidthDown.reset();
  627. this._avgPacketLossUp.reset();
  628. this._avgPacketLossDown.reset();
  629. this._avgPacketLossTotal.reset();
  630. this._avgRemoteFPS.reset();
  631. this._avgRemoteScreenFPS.reset();
  632. this._avgLocalFPS.reset();
  633. this._avgLocalScreenFPS.reset();
  634. this._avgCQ.reset();
  635. this._sampleIdx = 0;
  636. }
  637. /**
  638. * Unregisters all event listeners and stops working.
  639. */
  640. dispose() {
  641. this._conference.off(
  642. ConferenceEvents.P2P_STATUS,
  643. this._onP2PStatusChanged);
  644. this._conference.off(
  645. ConnectionQualityEvents.LOCAL_STATS_UPDATED,
  646. this._onLocalStatsUpdated);
  647. this.jvbStatsMonitor.dispose();
  648. this.p2pStatsMonitor.dispose();
  649. }
  650. }