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.

RTPStatsCollector.js 43KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. import { browsers } from '@jitsi/js-utils';
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import * as MediaType from '../../service/RTC/MediaType';
  4. import * as StatisticsEvents from '../../service/statistics/Events';
  5. import browser from '../browser';
  6. const GlobalOnErrorHandler = require('../util/GlobalOnErrorHandler');
  7. const logger = getLogger(__filename);
  8. /**
  9. * The lib-jitsi-meet browser-agnostic names of the browser-specific keys
  10. * reported by RTCPeerConnection#getStats mapped by browser.
  11. */
  12. const KEYS_BY_BROWSER_TYPE = {};
  13. KEYS_BY_BROWSER_TYPE[browsers.FIREFOX] = {
  14. 'ssrc': 'ssrc',
  15. 'packetsReceived': 'packetsReceived',
  16. 'packetsLost': 'packetsLost',
  17. 'packetsSent': 'packetsSent',
  18. 'bytesReceived': 'bytesReceived',
  19. 'bytesSent': 'bytesSent',
  20. 'framerateMean': 'framerateMean',
  21. 'ip': 'address',
  22. 'port': 'port',
  23. 'protocol': 'protocol'
  24. };
  25. KEYS_BY_BROWSER_TYPE[browsers.CHROME] = {
  26. 'receiveBandwidth': 'googAvailableReceiveBandwidth',
  27. 'sendBandwidth': 'googAvailableSendBandwidth',
  28. 'remoteAddress': 'googRemoteAddress',
  29. 'transportType': 'googTransportType',
  30. 'localAddress': 'googLocalAddress',
  31. 'activeConnection': 'googActiveConnection',
  32. 'ssrc': 'ssrc',
  33. 'packetsReceived': 'packetsReceived',
  34. 'packetsSent': 'packetsSent',
  35. 'packetsLost': 'packetsLost',
  36. 'bytesReceived': 'bytesReceived',
  37. 'bytesSent': 'bytesSent',
  38. 'googCodecName': 'googCodecName',
  39. 'googFrameHeightReceived': 'googFrameHeightReceived',
  40. 'googFrameWidthReceived': 'googFrameWidthReceived',
  41. 'googFrameHeightSent': 'googFrameHeightSent',
  42. 'googFrameWidthSent': 'googFrameWidthSent',
  43. 'googFrameRateReceived': 'googFrameRateReceived',
  44. 'googFrameRateSent': 'googFrameRateSent',
  45. 'audioInputLevel': 'audioInputLevel',
  46. 'audioOutputLevel': 'audioOutputLevel',
  47. 'currentRoundTripTime': 'googRtt',
  48. 'remoteCandidateType': 'googRemoteCandidateType',
  49. 'localCandidateType': 'googLocalCandidateType',
  50. 'ip': 'ip',
  51. 'port': 'port',
  52. 'protocol': 'protocol'
  53. };
  54. KEYS_BY_BROWSER_TYPE[browsers.OPERA]
  55. = KEYS_BY_BROWSER_TYPE[browsers.CHROME];
  56. KEYS_BY_BROWSER_TYPE[browsers.NWJS]
  57. = KEYS_BY_BROWSER_TYPE[browsers.CHROME];
  58. KEYS_BY_BROWSER_TYPE[browsers.ELECTRON]
  59. = KEYS_BY_BROWSER_TYPE[browsers.CHROME];
  60. KEYS_BY_BROWSER_TYPE[browsers.SAFARI]
  61. = KEYS_BY_BROWSER_TYPE[browsers.CHROME];
  62. KEYS_BY_BROWSER_TYPE[browsers.REACT_NATIVE]
  63. = KEYS_BY_BROWSER_TYPE[browsers.CHROME];
  64. /**
  65. * Calculates packet lost percent using the number of lost packets and the
  66. * number of all packet.
  67. * @param lostPackets the number of lost packets
  68. * @param totalPackets the number of all packets.
  69. * @returns {number} packet loss percent
  70. */
  71. function calculatePacketLoss(lostPackets, totalPackets) {
  72. if (!totalPackets || totalPackets <= 0
  73. || !lostPackets || lostPackets <= 0) {
  74. return 0;
  75. }
  76. return Math.round((lostPackets / totalPackets) * 100);
  77. }
  78. /**
  79. * Holds "statistics" for a single SSRC.
  80. * @constructor
  81. */
  82. function SsrcStats() {
  83. this.loss = {};
  84. this.bitrate = {
  85. download: 0,
  86. upload: 0
  87. };
  88. this.resolution = {};
  89. this.framerate = 0;
  90. this.codec = '';
  91. }
  92. /**
  93. * Sets the "loss" object.
  94. * @param loss the value to set.
  95. */
  96. SsrcStats.prototype.setLoss = function(loss) {
  97. this.loss = loss || {};
  98. };
  99. /**
  100. * Sets resolution that belong to the ssrc represented by this instance.
  101. * @param resolution new resolution value to be set.
  102. */
  103. SsrcStats.prototype.setResolution = function(resolution) {
  104. this.resolution = resolution || {};
  105. };
  106. /**
  107. * Adds the "download" and "upload" fields from the "bitrate" parameter to
  108. * the respective fields of the "bitrate" field of this object.
  109. * @param bitrate an object holding the values to add.
  110. */
  111. SsrcStats.prototype.addBitrate = function(bitrate) {
  112. this.bitrate.download += bitrate.download;
  113. this.bitrate.upload += bitrate.upload;
  114. };
  115. /**
  116. * Resets the bit rate for given <tt>ssrc</tt> that belong to the peer
  117. * represented by this instance.
  118. */
  119. SsrcStats.prototype.resetBitrate = function() {
  120. this.bitrate.download = 0;
  121. this.bitrate.upload = 0;
  122. };
  123. /**
  124. * Sets the "framerate".
  125. * @param framerate the value to set.
  126. */
  127. SsrcStats.prototype.setFramerate = function(framerate) {
  128. this.framerate = framerate || 0;
  129. };
  130. SsrcStats.prototype.setCodec = function(codec) {
  131. this.codec = codec || '';
  132. };
  133. /**
  134. *
  135. */
  136. function ConferenceStats() {
  137. /**
  138. * The bandwidth
  139. * @type {{}}
  140. */
  141. this.bandwidth = {};
  142. /**
  143. * The bit rate
  144. * @type {{}}
  145. */
  146. this.bitrate = {};
  147. /**
  148. * The packet loss rate
  149. * @type {{}}
  150. */
  151. this.packetLoss = null;
  152. /**
  153. * Array with the transport information.
  154. * @type {Array}
  155. */
  156. this.transport = [];
  157. }
  158. /* eslint-disable max-params */
  159. /**
  160. * <tt>StatsCollector</tt> registers for stats updates of given
  161. * <tt>peerconnection</tt> in given <tt>interval</tt>. On each update particular
  162. * stats are extracted and put in {@link SsrcStats} objects. Once the processing
  163. * is done <tt>audioLevelsUpdateCallback</tt> is called with <tt>this</tt>
  164. * instance as an event source.
  165. *
  166. * @param peerconnection WebRTC PeerConnection object.
  167. * @param audioLevelsInterval
  168. * @param statsInterval stats refresh interval given in ms.
  169. * @param eventEmitter
  170. * @constructor
  171. */
  172. export default function StatsCollector(
  173. peerconnection,
  174. audioLevelsInterval,
  175. statsInterval,
  176. eventEmitter) {
  177. // StatsCollector depends entirely on the format of the reports returned by
  178. // RTCPeerConnection#getStats. Given that the value of
  179. // browser#getName() is very unlikely to change at runtime, it
  180. // makes sense to discover whether StatsCollector supports the executing
  181. // browser as soon as possible. Otherwise, (1) getStatValue would have to
  182. // needlessly check a "static" condition multiple times very very often and
  183. // (2) the lack of support for the executing browser would be discovered and
  184. // reported multiple times very very often too late in the execution in some
  185. // totally unrelated callback.
  186. /**
  187. * The browser type supported by this StatsCollector. In other words, the
  188. * type of the browser which initialized this StatsCollector
  189. * instance.
  190. * @private
  191. */
  192. this._browserType = browser.getName();
  193. const keys = KEYS_BY_BROWSER_TYPE[this._browserType];
  194. if (!keys) {
  195. // eslint-disable-next-line no-throw-literal
  196. throw `The browser type '${this._browserType}' isn't supported!`;
  197. }
  198. /**
  199. * Whether to use the Promise-based getStats API or not.
  200. * @type {boolean}
  201. */
  202. this._usesPromiseGetStats
  203. = browser.isSafari() || browser.isFirefox();
  204. /**
  205. * The function which is to be used to retrieve the value associated in a
  206. * report returned by RTCPeerConnection#getStats with a lib-jitsi-meet
  207. * browser-agnostic name/key.
  208. *
  209. * @function
  210. * @private
  211. */
  212. this._getStatValue
  213. = this._usesPromiseGetStats
  214. ? this._defineNewGetStatValueMethod(keys)
  215. : this._defineGetStatValueMethod(keys);
  216. this.peerconnection = peerconnection;
  217. this.baselineAudioLevelsReport = null;
  218. this.currentAudioLevelsReport = null;
  219. this.currentStatsReport = null;
  220. this.previousStatsReport = null;
  221. this.audioLevelReportHistory = {};
  222. this.audioLevelsIntervalId = null;
  223. this.eventEmitter = eventEmitter;
  224. this.conferenceStats = new ConferenceStats();
  225. // Updates stats interval
  226. this.audioLevelsIntervalMilis = audioLevelsInterval;
  227. this.statsIntervalId = null;
  228. this.statsIntervalMilis = statsInterval;
  229. /**
  230. * Maps SSRC numbers to {@link SsrcStats}.
  231. * @type {Map<number,SsrcStats}
  232. */
  233. this.ssrc2stats = new Map();
  234. }
  235. /* eslint-enable max-params */
  236. /**
  237. * Stops stats updates.
  238. */
  239. StatsCollector.prototype.stop = function() {
  240. if (this.audioLevelsIntervalId) {
  241. clearInterval(this.audioLevelsIntervalId);
  242. this.audioLevelsIntervalId = null;
  243. }
  244. if (this.statsIntervalId) {
  245. clearInterval(this.statsIntervalId);
  246. this.statsIntervalId = null;
  247. }
  248. };
  249. /**
  250. * Callback passed to <tt>getStats</tt> method.
  251. * @param error an error that occurred on <tt>getStats</tt> call.
  252. */
  253. StatsCollector.prototype.errorCallback = function(error) {
  254. GlobalOnErrorHandler.callErrorHandler(error);
  255. logger.error('Get stats error', error);
  256. this.stop();
  257. };
  258. /**
  259. * Starts stats updates.
  260. */
  261. StatsCollector.prototype.start = function(startAudioLevelStats) {
  262. if (startAudioLevelStats) {
  263. if (browser.supportsReceiverStats()) {
  264. logger.info('Using RTCRtpSynchronizationSource for remote audio levels');
  265. }
  266. this.audioLevelsIntervalId = setInterval(
  267. () => {
  268. if (browser.supportsReceiverStats()) {
  269. const audioLevels = this.peerconnection.getAudioLevels();
  270. for (const ssrc in audioLevels) {
  271. if (audioLevels.hasOwnProperty(ssrc)) {
  272. // Use a scaling factor of 2.5 to report the same
  273. // audio levels that getStats reports.
  274. const audioLevel = audioLevels[ssrc] * 2.5;
  275. this.eventEmitter.emit(
  276. StatisticsEvents.AUDIO_LEVEL,
  277. this.peerconnection,
  278. Number.parseInt(ssrc, 10),
  279. audioLevel,
  280. false /* isLocal */);
  281. }
  282. }
  283. } else {
  284. // Interval updates
  285. this.peerconnection.getStats(
  286. report => {
  287. let results = null;
  288. if (!report || !report.result
  289. || typeof report.result !== 'function') {
  290. results = report;
  291. } else {
  292. results = report.result();
  293. }
  294. this.currentAudioLevelsReport = results;
  295. if (this._usesPromiseGetStats) {
  296. this.processNewAudioLevelReport();
  297. } else {
  298. this.processAudioLevelReport();
  299. }
  300. this.baselineAudioLevelsReport
  301. = this.currentAudioLevelsReport;
  302. },
  303. error => this.errorCallback(error)
  304. );
  305. }
  306. },
  307. this.audioLevelsIntervalMilis
  308. );
  309. }
  310. this.statsIntervalId = setInterval(
  311. () => {
  312. // Interval updates
  313. this.peerconnection.getStats(
  314. report => {
  315. let results = null;
  316. if (!report || !report.result
  317. || typeof report.result !== 'function') {
  318. // firefox
  319. results = report;
  320. } else {
  321. // chrome
  322. results = report.result();
  323. }
  324. this.currentStatsReport = results;
  325. try {
  326. if (this._usesPromiseGetStats) {
  327. this.processNewStatsReport();
  328. } else {
  329. this.processStatsReport();
  330. }
  331. } catch (e) {
  332. GlobalOnErrorHandler.callErrorHandler(e);
  333. logger.error(`Unsupported key:${e}`, e);
  334. }
  335. this.previousStatsReport = this.currentStatsReport;
  336. },
  337. error => this.errorCallback(error)
  338. );
  339. },
  340. this.statsIntervalMilis
  341. );
  342. };
  343. /**
  344. * Defines a function which (1) is to be used as a StatsCollector method and (2)
  345. * gets the value from a specific report returned by RTCPeerConnection#getStats
  346. * associated with a lib-jitsi-meet browser-agnostic name.
  347. *
  348. * @param {Object.<string,string>} keys the map of LibJitsi browser-agnostic
  349. * names to RTCPeerConnection#getStats browser-specific keys
  350. */
  351. StatsCollector.prototype._defineGetStatValueMethod = function(keys) {
  352. // Define the function which converts a lib-jitsi-meet browser-asnostic name
  353. // to a browser-specific key of a report returned by
  354. // RTCPeerConnection#getStats.
  355. const keyFromName = function(name) {
  356. const key = keys[name];
  357. if (key) {
  358. return key;
  359. }
  360. // eslint-disable-next-line no-throw-literal
  361. throw `The property '${name}' isn't supported!`;
  362. };
  363. // Define the function which retrieves the value from a specific report
  364. // returned by RTCPeerConnection#getStats associated with a given
  365. // browser-specific key.
  366. let itemStatByKey;
  367. switch (this._browserType) {
  368. case browsers.CHROME:
  369. case browsers.OPERA:
  370. case browsers.NWJS:
  371. case browsers.ELECTRON:
  372. // TODO What about other types of browser which are based on Chrome such
  373. // as NW.js? Every time we want to support a new type browser we have to
  374. // go and add more conditions (here and in multiple other places).
  375. // Cannot we do a feature detection instead of a browser type check? For
  376. // example, if item has a stat property of type function, then it's very
  377. // likely that whoever defined it wanted you to call it in order to
  378. // retrieve the value associated with a specific key.
  379. itemStatByKey = (item, key) => item.stat(key);
  380. break;
  381. case browsers.REACT_NATIVE:
  382. // The implementation provided by react-native-webrtc follows the
  383. // Objective-C WebRTC API: RTCStatsReport has a values property of type
  384. // Array in which each element is a key-value pair.
  385. itemStatByKey = function(item, key) {
  386. let value;
  387. item.values.some(pair => {
  388. if (pair.hasOwnProperty(key)) {
  389. value = pair[key];
  390. return true;
  391. }
  392. return false;
  393. });
  394. return value;
  395. };
  396. break;
  397. default:
  398. itemStatByKey = (item, key) => item[key];
  399. }
  400. // Compose the 2 functions defined above to get a function which retrieves
  401. // the value from a specific report returned by RTCPeerConnection#getStats
  402. // associated with a specific lib-jitsi-meet browser-agnostic name.
  403. return (item, name) => itemStatByKey(item, keyFromName(name));
  404. };
  405. /**
  406. * Obtains a stat value from given stat and converts it to a non-negative
  407. * number. If the value is either invalid or negative then 0 will be returned.
  408. * @param report
  409. * @param {string} name
  410. * @return {number}
  411. * @private
  412. */
  413. StatsCollector.prototype.getNonNegativeStat = function(report, name) {
  414. let value = this._getStatValue(report, name);
  415. if (typeof value !== 'number') {
  416. value = Number(value);
  417. }
  418. if (isNaN(value)) {
  419. return 0;
  420. }
  421. return Math.max(0, value);
  422. };
  423. /* eslint-disable no-continue */
  424. /**
  425. * Stats processing logic.
  426. */
  427. StatsCollector.prototype.processStatsReport = function() {
  428. if (!this.previousStatsReport) {
  429. return;
  430. }
  431. const getStatValue = this._getStatValue;
  432. const byteSentStats = {};
  433. for (const idx in this.currentStatsReport) {
  434. if (!this.currentStatsReport.hasOwnProperty(idx)) {
  435. continue;
  436. }
  437. const now = this.currentStatsReport[idx];
  438. // The browser API may return "undefined" values in the array
  439. if (!now) {
  440. continue;
  441. }
  442. try {
  443. const receiveBandwidth = getStatValue(now, 'receiveBandwidth');
  444. const sendBandwidth = getStatValue(now, 'sendBandwidth');
  445. if (receiveBandwidth || sendBandwidth) {
  446. this.conferenceStats.bandwidth = {
  447. 'download': Math.round(receiveBandwidth / 1000),
  448. 'upload': Math.round(sendBandwidth / 1000)
  449. };
  450. }
  451. } catch (e) { /* not supported*/ }
  452. if (now.type === 'googCandidatePair') {
  453. let active, ip, localCandidateType, localip,
  454. remoteCandidateType, rtt, type;
  455. try {
  456. active = getStatValue(now, 'activeConnection');
  457. if (!active) {
  458. continue;
  459. }
  460. ip = getStatValue(now, 'remoteAddress');
  461. type = getStatValue(now, 'transportType');
  462. localip = getStatValue(now, 'localAddress');
  463. localCandidateType = getStatValue(now, 'localCandidateType');
  464. remoteCandidateType = getStatValue(now, 'remoteCandidateType');
  465. rtt = this.getNonNegativeStat(now, 'currentRoundTripTime');
  466. } catch (e) { /* not supported*/ }
  467. if (!ip || !type || !localip || active !== 'true') {
  468. continue;
  469. }
  470. // Save the address unless it has been saved already.
  471. const conferenceStatsTransport = this.conferenceStats.transport;
  472. if (!conferenceStatsTransport.some(
  473. t =>
  474. t.ip === ip
  475. && t.type === type
  476. && t.localip === localip)) {
  477. conferenceStatsTransport.push({
  478. ip,
  479. type,
  480. localip,
  481. p2p: this.peerconnection.isP2P,
  482. localCandidateType,
  483. remoteCandidateType,
  484. rtt
  485. });
  486. }
  487. continue;
  488. }
  489. if (now.type === 'candidatepair') {
  490. // we need succeeded and selected pairs only
  491. if (now.state !== 'succeeded' || !now.selected) {
  492. continue;
  493. }
  494. const local = this.currentStatsReport[now.localCandidateId];
  495. const remote = this.currentStatsReport[now.remoteCandidateId];
  496. this.conferenceStats.transport.push({
  497. ip: `${remote.ipAddress}:${remote.portNumber}`,
  498. type: local.transport,
  499. localip: `${local.ipAddress}:${local.portNumber}`,
  500. p2p: this.peerconnection.isP2P,
  501. localCandidateType: local.candidateType,
  502. remoteCandidateType: remote.candidateType
  503. });
  504. }
  505. if (now.type !== 'ssrc' && now.type !== 'outboundrtp'
  506. && now.type !== 'inboundrtp' && now.type !== 'track') {
  507. continue;
  508. }
  509. const before = this.previousStatsReport[idx];
  510. let ssrc = this.getNonNegativeStat(now, 'ssrc');
  511. // If type="track", take the first SSRC from ssrcIds.
  512. if (now.type === 'track' && Array.isArray(now.ssrcIds)) {
  513. ssrc = Number(now.ssrcIds[0]);
  514. }
  515. if (!before || !ssrc) {
  516. continue;
  517. }
  518. // isRemote is available only in FF and is ignored in case of chrome
  519. // according to the spec
  520. // https://www.w3.org/TR/webrtc-stats/#dom-rtcrtpstreamstats-isremote
  521. // when isRemote is true indicates that the measurements were done at
  522. // the remote endpoint and reported in an RTCP RR/XR.
  523. // Fixes a problem where we are calculating local stats wrong adding
  524. // the sent bytes to the local download bitrate.
  525. // In new W3 stats spec, type="track" has a remoteSource boolean
  526. // property.
  527. if (now.isRemote === true || now.remoteSource === true) {
  528. continue;
  529. }
  530. let ssrcStats = this.ssrc2stats.get(ssrc);
  531. if (!ssrcStats) {
  532. ssrcStats = new SsrcStats();
  533. this.ssrc2stats.set(ssrc, ssrcStats);
  534. }
  535. let isDownloadStream = true;
  536. let key = 'packetsReceived';
  537. let packetsNow = getStatValue(now, key);
  538. if (typeof packetsNow === 'undefined'
  539. || packetsNow === null || packetsNow === '') {
  540. isDownloadStream = false;
  541. key = 'packetsSent';
  542. packetsNow = getStatValue(now, key);
  543. if (typeof packetsNow === 'undefined' || packetsNow === null) {
  544. logger.warn('No packetsReceived nor packetsSent stat found');
  545. }
  546. }
  547. if (!packetsNow || packetsNow < 0) {
  548. packetsNow = 0;
  549. }
  550. const packetsBefore = this.getNonNegativeStat(before, key);
  551. const packetsDiff = Math.max(0, packetsNow - packetsBefore);
  552. const packetsLostNow
  553. = this.getNonNegativeStat(now, 'packetsLost');
  554. const packetsLostBefore
  555. = this.getNonNegativeStat(before, 'packetsLost');
  556. const packetsLostDiff = Math.max(0, packetsLostNow - packetsLostBefore);
  557. ssrcStats.setLoss({
  558. packetsTotal: packetsDiff + packetsLostDiff,
  559. packetsLost: packetsLostDiff,
  560. isDownloadStream
  561. });
  562. const bytesReceivedNow
  563. = this.getNonNegativeStat(now, 'bytesReceived');
  564. const bytesReceivedBefore
  565. = this.getNonNegativeStat(before, 'bytesReceived');
  566. const bytesReceived
  567. = Math.max(0, bytesReceivedNow - bytesReceivedBefore);
  568. let bytesSent = 0;
  569. // TODO: clean this mess up!
  570. let nowBytesTransmitted = getStatValue(now, 'bytesSent');
  571. if (typeof nowBytesTransmitted === 'number'
  572. || typeof nowBytesTransmitted === 'string') {
  573. nowBytesTransmitted = Number(nowBytesTransmitted);
  574. if (!isNaN(nowBytesTransmitted)) {
  575. byteSentStats[ssrc] = nowBytesTransmitted;
  576. if (nowBytesTransmitted > 0) {
  577. bytesSent = nowBytesTransmitted
  578. - getStatValue(before, 'bytesSent');
  579. }
  580. }
  581. }
  582. bytesSent = Math.max(0, bytesSent);
  583. const timeMs = now.timestamp - before.timestamp;
  584. let bitrateReceivedKbps = 0, bitrateSentKbps = 0;
  585. if (timeMs > 0) {
  586. // TODO is there any reason to round here?
  587. bitrateReceivedKbps = Math.round((bytesReceived * 8) / timeMs);
  588. bitrateSentKbps = Math.round((bytesSent * 8) / timeMs);
  589. }
  590. ssrcStats.addBitrate({
  591. 'download': bitrateReceivedKbps,
  592. 'upload': bitrateSentKbps
  593. });
  594. const resolution = {
  595. height: null,
  596. width: null
  597. };
  598. try {
  599. let height, width;
  600. if ((height = getStatValue(now, 'googFrameHeightReceived'))
  601. && (width = getStatValue(now, 'googFrameWidthReceived'))) {
  602. resolution.height = height;
  603. resolution.width = width;
  604. } else if ((height = getStatValue(now, 'googFrameHeightSent'))
  605. && (width = getStatValue(now, 'googFrameWidthSent'))) {
  606. resolution.height = height;
  607. resolution.width = width;
  608. }
  609. } catch (e) { /* not supported*/ }
  610. // Tries to get frame rate
  611. let frameRate;
  612. try {
  613. frameRate = getStatValue(now, 'googFrameRateReceived')
  614. || getStatValue(now, 'googFrameRateSent') || 0;
  615. } catch (e) {
  616. // if it fails with previous properties(chrome),
  617. // let's try with another one (FF)
  618. try {
  619. frameRate = this.getNonNegativeStat(now, 'framerateMean');
  620. } catch (err) { /* not supported*/ }
  621. }
  622. ssrcStats.setFramerate(Math.round(frameRate || 0));
  623. if (resolution.height && resolution.width) {
  624. ssrcStats.setResolution(resolution);
  625. } else {
  626. ssrcStats.setResolution(null);
  627. }
  628. let codec;
  629. // Try to get the codec for later reporting.
  630. try {
  631. codec = getStatValue(now, 'googCodecName') || '';
  632. } catch (e) { /* not supported*/ }
  633. ssrcStats.setCodec(codec);
  634. }
  635. this.eventEmitter.emit(
  636. StatisticsEvents.BYTE_SENT_STATS, this.peerconnection, byteSentStats);
  637. this._processAndEmitReport();
  638. };
  639. /**
  640. *
  641. */
  642. StatsCollector.prototype._processAndEmitReport = function() {
  643. // process stats
  644. const totalPackets = {
  645. download: 0,
  646. upload: 0
  647. };
  648. const lostPackets = {
  649. download: 0,
  650. upload: 0
  651. };
  652. let bitrateDownload = 0;
  653. let bitrateUpload = 0;
  654. const resolutions = {};
  655. const framerates = {};
  656. const codecs = {};
  657. let audioBitrateDownload = 0;
  658. let audioBitrateUpload = 0;
  659. let audioCodec = '';
  660. let videoBitrateDownload = 0;
  661. let videoBitrateUpload = 0;
  662. let videoCodec = '';
  663. for (const [ ssrc, ssrcStats ] of this.ssrc2stats) {
  664. // process packet loss stats
  665. const loss = ssrcStats.loss;
  666. const type = loss.isDownloadStream ? 'download' : 'upload';
  667. totalPackets[type] += loss.packetsTotal;
  668. lostPackets[type] += loss.packetsLost;
  669. // process bitrate stats
  670. bitrateDownload += ssrcStats.bitrate.download;
  671. bitrateUpload += ssrcStats.bitrate.upload;
  672. // collect resolutions and framerates
  673. const track = this.peerconnection.getTrackBySSRC(ssrc);
  674. if (track) {
  675. if (track.isAudioTrack()) {
  676. audioBitrateDownload += ssrcStats.bitrate.download;
  677. audioBitrateUpload += ssrcStats.bitrate.upload;
  678. audioCodec = ssrcStats.codec;
  679. } else {
  680. videoBitrateDownload += ssrcStats.bitrate.download;
  681. videoBitrateUpload += ssrcStats.bitrate.upload;
  682. videoCodec = ssrcStats.codec;
  683. }
  684. const participantId = track.getParticipantId();
  685. if (participantId) {
  686. const resolution = ssrcStats.resolution;
  687. if (resolution.width
  688. && resolution.height
  689. && resolution.width !== -1
  690. && resolution.height !== -1) {
  691. const userResolutions = resolutions[participantId] || {};
  692. userResolutions[ssrc] = resolution;
  693. resolutions[participantId] = userResolutions;
  694. }
  695. if (ssrcStats.framerate !== 0) {
  696. const userFramerates = framerates[participantId] || {};
  697. userFramerates[ssrc] = ssrcStats.framerate;
  698. framerates[participantId] = userFramerates;
  699. }
  700. if (audioCodec.length && videoCodec.length) {
  701. const codecDesc = {
  702. 'audio': audioCodec,
  703. 'video': videoCodec
  704. };
  705. const userCodecs = codecs[participantId] || {};
  706. userCodecs[ssrc] = codecDesc;
  707. codecs[participantId] = userCodecs;
  708. }
  709. } else {
  710. logger.error(`No participant ID returned by ${track}`);
  711. }
  712. }
  713. ssrcStats.resetBitrate();
  714. }
  715. this.conferenceStats.bitrate = {
  716. 'upload': bitrateUpload,
  717. 'download': bitrateDownload
  718. };
  719. this.conferenceStats.bitrate.audio = {
  720. 'upload': audioBitrateUpload,
  721. 'download': audioBitrateDownload
  722. };
  723. this.conferenceStats.bitrate.video = {
  724. 'upload': videoBitrateUpload,
  725. 'download': videoBitrateDownload
  726. };
  727. this.conferenceStats.packetLoss = {
  728. total:
  729. calculatePacketLoss(
  730. lostPackets.download + lostPackets.upload,
  731. totalPackets.download + totalPackets.upload),
  732. download:
  733. calculatePacketLoss(lostPackets.download, totalPackets.download),
  734. upload:
  735. calculatePacketLoss(lostPackets.upload, totalPackets.upload)
  736. };
  737. const avgAudioLevels = {};
  738. let localAvgAudioLevels;
  739. Object.keys(this.audioLevelReportHistory).forEach(ssrc => {
  740. const { data, isLocal } = this.audioLevelReportHistory[ssrc];
  741. const avgAudioLevel = data.reduce((sum, currentValue) => sum + currentValue) / data.length;
  742. if (isLocal) {
  743. localAvgAudioLevels = avgAudioLevel;
  744. } else {
  745. const track = this.peerconnection.getTrackBySSRC(Number(ssrc));
  746. if (track) {
  747. const participantId = track.getParticipantId();
  748. if (participantId) {
  749. avgAudioLevels[participantId] = avgAudioLevel;
  750. }
  751. }
  752. }
  753. });
  754. this.audioLevelReportHistory = {};
  755. this.eventEmitter.emit(
  756. StatisticsEvents.CONNECTION_STATS,
  757. this.peerconnection,
  758. {
  759. 'bandwidth': this.conferenceStats.bandwidth,
  760. 'bitrate': this.conferenceStats.bitrate,
  761. 'packetLoss': this.conferenceStats.packetLoss,
  762. 'resolution': resolutions,
  763. 'framerate': framerates,
  764. 'codec': codecs,
  765. 'transport': this.conferenceStats.transport,
  766. localAvgAudioLevels,
  767. avgAudioLevels
  768. });
  769. this.conferenceStats.transport = [];
  770. };
  771. /**
  772. * Stats processing logic.
  773. */
  774. StatsCollector.prototype.processAudioLevelReport = function() {
  775. if (!this.baselineAudioLevelsReport) {
  776. return;
  777. }
  778. const getStatValue = this._getStatValue;
  779. for (const idx in this.currentAudioLevelsReport) {
  780. if (!this.currentAudioLevelsReport.hasOwnProperty(idx)) {
  781. continue;
  782. }
  783. const now = this.currentAudioLevelsReport[idx];
  784. if (now.type !== 'ssrc' && now.type !== 'track') {
  785. continue;
  786. }
  787. const before = this.baselineAudioLevelsReport[idx];
  788. let ssrc = this.getNonNegativeStat(now, 'ssrc');
  789. if (!ssrc && Array.isArray(now.ssrcIds)) {
  790. ssrc = Number(now.ssrcIds[0]);
  791. }
  792. if (!before) {
  793. logger.warn(`${ssrc} not enough data`);
  794. continue;
  795. }
  796. if (!ssrc) {
  797. if ((Date.now() - now.timestamp) < 3000) {
  798. logger.warn('No ssrc: ');
  799. }
  800. continue;
  801. }
  802. // Audio level
  803. let audioLevel;
  804. try {
  805. audioLevel
  806. = getStatValue(now, 'audioInputLevel')
  807. || getStatValue(now, 'audioOutputLevel');
  808. } catch (e) { /* not supported*/
  809. logger.warn('Audio Levels are not available in the statistics.');
  810. clearInterval(this.audioLevelsIntervalId);
  811. return;
  812. }
  813. if (audioLevel) {
  814. let isLocal;
  815. // If type="ssrc" (legacy) check whether they are received packets.
  816. if (now.type === 'ssrc') {
  817. isLocal = !getStatValue(now, 'packetsReceived');
  818. // If type="track", check remoteSource boolean property.
  819. } else {
  820. isLocal = !now.remoteSource;
  821. }
  822. // According to the W3C WebRTC Stats spec, audioLevel should be in
  823. // 0..1 range (0 == silence). However browsers don't behave that
  824. // way so we must convert it to 0..1.
  825. // TODO: Can't find specs about what this value really is, but it
  826. // seems to vary between 0 and around 32k.
  827. audioLevel = audioLevel / 32767;
  828. if (!(ssrc in this.audioLevelReportHistory)) {
  829. this.audioLevelReportHistory[ssrc] = {
  830. isLocal,
  831. data: []
  832. };
  833. }
  834. this.audioLevelReportHistory[ssrc].data.push(audioLevel);
  835. this.eventEmitter.emit(
  836. StatisticsEvents.AUDIO_LEVEL,
  837. this.peerconnection,
  838. ssrc,
  839. audioLevel,
  840. isLocal);
  841. }
  842. }
  843. };
  844. /* eslint-enable no-continue */
  845. /**
  846. * New promised based getStats report processing.
  847. * Tested with chrome, firefox and safari. Not switching it on for chrome as
  848. * frameRate stat is missing and calculating it using framesSent,
  849. * gives values double the values seen in webrtc-internals.
  850. * https://w3c.github.io/webrtc-stats/
  851. */
  852. /**
  853. * Defines a function which (1) is to be used as a StatsCollector method and (2)
  854. * gets the value from a specific report returned by RTCPeerConnection#getStats
  855. * associated with a lib-jitsi-meet browser-agnostic name in case of using
  856. * Promised based getStats.
  857. *
  858. * @param {Object.<string,string>} keys the map of LibJitsi browser-agnostic
  859. * names to RTCPeerConnection#getStats browser-specific keys
  860. */
  861. StatsCollector.prototype._defineNewGetStatValueMethod = function(keys) {
  862. // Define the function which converts a lib-jitsi-meet browser-asnostic name
  863. // to a browser-specific key of a report returned by
  864. // RTCPeerConnection#getStats.
  865. const keyFromName = function(name) {
  866. const key = keys[name];
  867. if (key) {
  868. return key;
  869. }
  870. // eslint-disable-next-line no-throw-literal
  871. throw `The property '${name}' isn't supported!`;
  872. };
  873. // Compose the 2 functions defined above to get a function which retrieves
  874. // the value from a specific report returned by RTCPeerConnection#getStats
  875. // associated with a specific lib-jitsi-meet browser-agnostic name.
  876. return (item, name) => item[keyFromName(name)];
  877. };
  878. /**
  879. * Converts the value to a non-negative number.
  880. * If the value is either invalid or negative then 0 will be returned.
  881. * @param {*} v
  882. * @return {number}
  883. * @private
  884. */
  885. StatsCollector.prototype.getNonNegativeValue = function(v) {
  886. let value = v;
  887. if (typeof value !== 'number') {
  888. value = Number(value);
  889. }
  890. if (isNaN(value)) {
  891. return 0;
  892. }
  893. return Math.max(0, value);
  894. };
  895. /**
  896. * Calculates bitrate between before and now using a supplied field name and its
  897. * value in the stats.
  898. * @param {RTCInboundRtpStreamStats|RTCSentRtpStreamStats} now the current stats
  899. * @param {RTCInboundRtpStreamStats|RTCSentRtpStreamStats} before the
  900. * previous stats.
  901. * @param fieldName the field to use for calculations.
  902. * @return {number} the calculated bitrate between now and before.
  903. * @private
  904. */
  905. StatsCollector.prototype._calculateBitrate = function(now, before, fieldName) {
  906. const bytesNow = this.getNonNegativeValue(now[fieldName]);
  907. const bytesBefore = this.getNonNegativeValue(before[fieldName]);
  908. const bytesProcessed = Math.max(0, bytesNow - bytesBefore);
  909. const timeMs = now.timestamp - before.timestamp;
  910. let bitrateKbps = 0;
  911. if (timeMs > 0) {
  912. // TODO is there any reason to round here?
  913. bitrateKbps = Math.round((bytesProcessed * 8) / timeMs);
  914. }
  915. return bitrateKbps;
  916. };
  917. /**
  918. * Stats processing new getStats logic.
  919. */
  920. StatsCollector.prototype.processNewStatsReport = function() {
  921. if (!this.previousStatsReport) {
  922. return;
  923. }
  924. const getStatValue = this._getStatValue;
  925. const byteSentStats = {};
  926. this.currentStatsReport.forEach(now => {
  927. // RTCIceCandidatePairStats
  928. // https://w3c.github.io/webrtc-stats/#candidatepair-dict*
  929. if (now.type === 'candidate-pair'
  930. && now.nominated
  931. && now.state === 'succeeded') {
  932. const availableIncomingBitrate = now.availableIncomingBitrate;
  933. const availableOutgoingBitrate = now.availableOutgoingBitrate;
  934. if (availableIncomingBitrate || availableOutgoingBitrate) {
  935. this.conferenceStats.bandwidth = {
  936. 'download': Math.round(availableIncomingBitrate / 1000),
  937. 'upload': Math.round(availableOutgoingBitrate / 1000)
  938. };
  939. }
  940. const remoteUsedCandidate
  941. = this.currentStatsReport.get(now.remoteCandidateId);
  942. const localUsedCandidate
  943. = this.currentStatsReport.get(now.localCandidateId);
  944. // RTCIceCandidateStats
  945. // https://w3c.github.io/webrtc-stats/#icecandidate-dict*
  946. // safari currently does not provide ice candidates in stats
  947. if (remoteUsedCandidate && localUsedCandidate) {
  948. const remoteIpAddress = getStatValue(remoteUsedCandidate, 'ip');
  949. const remotePort = getStatValue(remoteUsedCandidate, 'port');
  950. const ip = `${remoteIpAddress}:${remotePort}`;
  951. const localIpAddress = getStatValue(localUsedCandidate, 'ip');
  952. const localPort = getStatValue(localUsedCandidate, 'port');
  953. const localIp = `${localIpAddress}:${localPort}`;
  954. const type = getStatValue(remoteUsedCandidate, 'protocol');
  955. // Save the address unless it has been saved already.
  956. const conferenceStatsTransport = this.conferenceStats.transport;
  957. if (!conferenceStatsTransport.some(
  958. t =>
  959. t.ip === ip
  960. && t.type === type
  961. && t.localip === localIp)) {
  962. conferenceStatsTransport.push({
  963. ip,
  964. type,
  965. localIp,
  966. p2p: this.peerconnection.isP2P,
  967. localCandidateType: localUsedCandidate.candidateType,
  968. remoteCandidateType: remoteUsedCandidate.candidateType,
  969. networkType: localUsedCandidate.networkType,
  970. rtt: now.currentRoundTripTime * 1000
  971. });
  972. }
  973. }
  974. // RTCReceivedRtpStreamStats
  975. // https://w3c.github.io/webrtc-stats/#receivedrtpstats-dict*
  976. // RTCSentRtpStreamStats
  977. // https://w3c.github.io/webrtc-stats/#sentrtpstats-dict*
  978. } else if (now.type === 'inbound-rtp' || now.type === 'outbound-rtp') {
  979. const before = this.previousStatsReport.get(now.id);
  980. const ssrc = this.getNonNegativeValue(now.ssrc);
  981. if (!before || !ssrc) {
  982. return;
  983. }
  984. let ssrcStats = this.ssrc2stats.get(ssrc);
  985. if (!ssrcStats) {
  986. ssrcStats = new SsrcStats();
  987. this.ssrc2stats.set(ssrc, ssrcStats);
  988. }
  989. let isDownloadStream = true;
  990. let key = 'packetsReceived';
  991. if (now.type === 'outbound-rtp') {
  992. isDownloadStream = false;
  993. key = 'packetsSent';
  994. }
  995. let packetsNow = now[key];
  996. if (!packetsNow || packetsNow < 0) {
  997. packetsNow = 0;
  998. }
  999. const packetsBefore = this.getNonNegativeValue(before[key]);
  1000. const packetsDiff = Math.max(0, packetsNow - packetsBefore);
  1001. const packetsLostNow
  1002. = this.getNonNegativeValue(now.packetsLost);
  1003. const packetsLostBefore
  1004. = this.getNonNegativeValue(before.packetsLost);
  1005. const packetsLostDiff
  1006. = Math.max(0, packetsLostNow - packetsLostBefore);
  1007. ssrcStats.setLoss({
  1008. packetsTotal: packetsDiff + packetsLostDiff,
  1009. packetsLost: packetsLostDiff,
  1010. isDownloadStream
  1011. });
  1012. if (now.type === 'inbound-rtp') {
  1013. ssrcStats.addBitrate({
  1014. 'download': this._calculateBitrate(
  1015. now, before, 'bytesReceived'),
  1016. 'upload': 0
  1017. });
  1018. // RTCInboundRtpStreamStats
  1019. // https://w3c.github.io/webrtc-stats/#inboundrtpstats-dict*
  1020. // TODO: can we use framesDecoded for frame rate, available
  1021. // in chrome
  1022. } else {
  1023. byteSentStats[ssrc] = this.getNonNegativeValue(now.bytesSent);
  1024. ssrcStats.addBitrate({
  1025. 'download': 0,
  1026. 'upload': this._calculateBitrate(
  1027. now, before, 'bytesSent')
  1028. });
  1029. // RTCOutboundRtpStreamStats
  1030. // https://w3c.github.io/webrtc-stats/#outboundrtpstats-dict*
  1031. // TODO: can we use framesEncoded for frame rate, available
  1032. // in chrome
  1033. }
  1034. // FF has framerateMean out of spec
  1035. const framerateMean = now.framerateMean;
  1036. if (framerateMean) {
  1037. ssrcStats.setFramerate(Math.round(framerateMean || 0));
  1038. }
  1039. // track for resolution
  1040. // RTCVideoHandlerStats
  1041. // https://w3c.github.io/webrtc-stats/#vststats-dict*
  1042. // RTCMediaHandlerStats
  1043. // https://w3c.github.io/webrtc-stats/#mststats-dict*
  1044. } else if (now.type === 'track') {
  1045. const resolution = {
  1046. height: now.frameHeight,
  1047. width: now.frameWidth
  1048. };
  1049. // Tries to get frame rate
  1050. let frameRate = now.framesPerSecond;
  1051. if (!frameRate) {
  1052. // we need to calculate it
  1053. const before = this.previousStatsReport.get(now.id);
  1054. if (before) {
  1055. const timeMs = now.timestamp - before.timestamp;
  1056. if (timeMs > 0 && now.framesSent) {
  1057. const numberOfFramesSinceBefore
  1058. = now.framesSent - before.framesSent;
  1059. frameRate = (numberOfFramesSinceBefore / timeMs) * 1000;
  1060. }
  1061. }
  1062. if (!frameRate) {
  1063. return;
  1064. }
  1065. }
  1066. const trackIdentifier = now.trackIdentifier;
  1067. const ssrc = this.peerconnection.getSsrcByTrackId(trackIdentifier);
  1068. if (!ssrc) {
  1069. return;
  1070. }
  1071. let ssrcStats = this.ssrc2stats.get(ssrc);
  1072. if (!ssrcStats) {
  1073. ssrcStats = new SsrcStats();
  1074. this.ssrc2stats.set(ssrc, ssrcStats);
  1075. }
  1076. ssrcStats.setFramerate(Math.round(frameRate || 0));
  1077. if (resolution.height && resolution.width) {
  1078. ssrcStats.setResolution(resolution);
  1079. } else {
  1080. ssrcStats.setResolution(null);
  1081. }
  1082. }
  1083. });
  1084. this.eventEmitter.emit(
  1085. StatisticsEvents.BYTE_SENT_STATS, this.peerconnection, byteSentStats);
  1086. this._processAndEmitReport();
  1087. };
  1088. /**
  1089. * Stats processing logic.
  1090. */
  1091. StatsCollector.prototype.processNewAudioLevelReport = function() {
  1092. if (!this.baselineAudioLevelsReport) {
  1093. return;
  1094. }
  1095. this.currentAudioLevelsReport.forEach(now => {
  1096. if (now.type !== 'track') {
  1097. return;
  1098. }
  1099. // Audio level
  1100. const audioLevel = now.audioLevel;
  1101. if (!audioLevel) {
  1102. return;
  1103. }
  1104. const trackIdentifier = now.trackIdentifier;
  1105. const ssrc = this.peerconnection.getSsrcByTrackId(trackIdentifier);
  1106. if (ssrc) {
  1107. const isLocal
  1108. = ssrc === this.peerconnection.getLocalSSRC(
  1109. this.peerconnection.getLocalTracks(MediaType.AUDIO));
  1110. this.eventEmitter.emit(
  1111. StatisticsEvents.AUDIO_LEVEL,
  1112. this.peerconnection,
  1113. ssrc,
  1114. audioLevel,
  1115. isLocal);
  1116. }
  1117. });
  1118. };
  1119. /**
  1120. * End new promised based getStats processing methods.
  1121. */