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

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