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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. import browser from '../browser';
  2. import { browsers } from 'js-utils';
  3. import * as StatisticsEvents from '../../service/statistics/Events';
  4. import * as MediaType from '../../service/RTC/MediaType';
  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. const self = this;
  262. if (startAudioLevelStats) {
  263. this.audioLevelsIntervalId = setInterval(
  264. () => {
  265. // Interval updates
  266. self.peerconnection.getStats(
  267. report => {
  268. let results = null;
  269. if (!report || !report.result
  270. || typeof report.result !== 'function') {
  271. results = report;
  272. } else {
  273. results = report.result();
  274. }
  275. self.currentAudioLevelsReport = results;
  276. if (this._usesPromiseGetStats) {
  277. self.processNewAudioLevelReport();
  278. } else {
  279. self.processAudioLevelReport();
  280. }
  281. self.baselineAudioLevelsReport
  282. = self.currentAudioLevelsReport;
  283. },
  284. error => self.errorCallback(error)
  285. );
  286. },
  287. self.audioLevelsIntervalMilis
  288. );
  289. }
  290. this.statsIntervalId = setInterval(
  291. () => {
  292. // Interval updates
  293. self.peerconnection.getStats(
  294. report => {
  295. let results = null;
  296. if (!report || !report.result
  297. || typeof report.result !== 'function') {
  298. // firefox
  299. results = report;
  300. } else {
  301. // chrome
  302. results = report.result();
  303. }
  304. self.currentStatsReport = results;
  305. try {
  306. if (this._usesPromiseGetStats) {
  307. self.processNewStatsReport();
  308. } else {
  309. self.processStatsReport();
  310. }
  311. } catch (e) {
  312. GlobalOnErrorHandler.callErrorHandler(e);
  313. logger.error(`Unsupported key:${e}`, e);
  314. }
  315. self.previousStatsReport = self.currentStatsReport;
  316. },
  317. error => self.errorCallback(error)
  318. );
  319. },
  320. self.statsIntervalMilis
  321. );
  322. };
  323. /**
  324. * Defines a function which (1) is to be used as a StatsCollector method and (2)
  325. * gets the value from a specific report returned by RTCPeerConnection#getStats
  326. * associated with a lib-jitsi-meet browser-agnostic name.
  327. *
  328. * @param {Object.<string,string>} keys the map of LibJitsi browser-agnostic
  329. * names to RTCPeerConnection#getStats browser-specific keys
  330. */
  331. StatsCollector.prototype._defineGetStatValueMethod = function(keys) {
  332. // Define the function which converts a lib-jitsi-meet browser-asnostic name
  333. // to a browser-specific key of a report returned by
  334. // RTCPeerConnection#getStats.
  335. const keyFromName = function(name) {
  336. const key = keys[name];
  337. if (key) {
  338. return key;
  339. }
  340. // eslint-disable-next-line no-throw-literal
  341. throw `The property '${name}' isn't supported!`;
  342. };
  343. // Define the function which retrieves the value from a specific report
  344. // returned by RTCPeerConnection#getStats associated with a given
  345. // browser-specific key.
  346. let itemStatByKey;
  347. switch (this._browserType) {
  348. case browsers.CHROME:
  349. case browsers.OPERA:
  350. case browsers.NWJS:
  351. case browsers.ELECTRON:
  352. // TODO What about other types of browser which are based on Chrome such
  353. // as NW.js? Every time we want to support a new type browser we have to
  354. // go and add more conditions (here and in multiple other places).
  355. // Cannot we do a feature detection instead of a browser type check? For
  356. // example, if item has a stat property of type function, then it's very
  357. // likely that whoever defined it wanted you to call it in order to
  358. // retrieve the value associated with a specific key.
  359. itemStatByKey = (item, key) => item.stat(key);
  360. break;
  361. case browsers.REACT_NATIVE:
  362. // The implementation provided by react-native-webrtc follows the
  363. // Objective-C WebRTC API: RTCStatsReport has a values property of type
  364. // Array in which each element is a key-value pair.
  365. itemStatByKey = function(item, key) {
  366. let value;
  367. item.values.some(pair => {
  368. if (pair.hasOwnProperty(key)) {
  369. value = pair[key];
  370. return true;
  371. }
  372. return false;
  373. });
  374. return value;
  375. };
  376. break;
  377. default:
  378. itemStatByKey = (item, key) => item[key];
  379. }
  380. // Compose the 2 functions defined above to get a function which retrieves
  381. // the value from a specific report returned by RTCPeerConnection#getStats
  382. // associated with a specific lib-jitsi-meet browser-agnostic name.
  383. return (item, name) => itemStatByKey(item, keyFromName(name));
  384. };
  385. /**
  386. * Obtains a stat value from given stat and converts it to a non-negative
  387. * number. If the value is either invalid or negative then 0 will be returned.
  388. * @param report
  389. * @param {string} name
  390. * @return {number}
  391. * @private
  392. */
  393. StatsCollector.prototype.getNonNegativeStat = function(report, name) {
  394. let value = this._getStatValue(report, name);
  395. if (typeof value !== 'number') {
  396. value = Number(value);
  397. }
  398. if (isNaN(value)) {
  399. return 0;
  400. }
  401. return Math.max(0, value);
  402. };
  403. /* eslint-disable no-continue */
  404. /**
  405. * Stats processing logic.
  406. */
  407. StatsCollector.prototype.processStatsReport = function() {
  408. if (!this.previousStatsReport) {
  409. return;
  410. }
  411. const getStatValue = this._getStatValue;
  412. const byteSentStats = {};
  413. for (const idx in this.currentStatsReport) {
  414. if (!this.currentStatsReport.hasOwnProperty(idx)) {
  415. continue;
  416. }
  417. const now = this.currentStatsReport[idx];
  418. // The browser API may return "undefined" values in the array
  419. if (!now) {
  420. continue;
  421. }
  422. try {
  423. const receiveBandwidth = getStatValue(now, 'receiveBandwidth');
  424. const sendBandwidth = getStatValue(now, 'sendBandwidth');
  425. if (receiveBandwidth || sendBandwidth) {
  426. this.conferenceStats.bandwidth = {
  427. 'download': Math.round(receiveBandwidth / 1000),
  428. 'upload': Math.round(sendBandwidth / 1000)
  429. };
  430. }
  431. } catch (e) { /* not supported*/ }
  432. if (now.type === 'googCandidatePair') {
  433. let active, ip, localCandidateType, localip,
  434. remoteCandidateType, rtt, type;
  435. try {
  436. active = getStatValue(now, 'activeConnection');
  437. if (!active) {
  438. continue;
  439. }
  440. ip = getStatValue(now, 'remoteAddress');
  441. type = getStatValue(now, 'transportType');
  442. localip = getStatValue(now, 'localAddress');
  443. localCandidateType = getStatValue(now, 'localCandidateType');
  444. remoteCandidateType = getStatValue(now, 'remoteCandidateType');
  445. rtt = this.getNonNegativeStat(now, 'currentRoundTripTime');
  446. } catch (e) { /* not supported*/ }
  447. if (!ip || !type || !localip || active !== 'true') {
  448. continue;
  449. }
  450. // Save the address unless it has been saved already.
  451. const conferenceStatsTransport = this.conferenceStats.transport;
  452. if (!conferenceStatsTransport.some(
  453. t =>
  454. t.ip === ip
  455. && t.type === type
  456. && t.localip === localip)) {
  457. conferenceStatsTransport.push({
  458. ip,
  459. type,
  460. localip,
  461. p2p: this.peerconnection.isP2P,
  462. localCandidateType,
  463. remoteCandidateType,
  464. rtt
  465. });
  466. }
  467. continue;
  468. }
  469. if (now.type === 'candidatepair') {
  470. // we need succeeded and selected pairs only
  471. if (now.state !== 'succeeded' || !now.selected) {
  472. continue;
  473. }
  474. const local = this.currentStatsReport[now.localCandidateId];
  475. const remote = this.currentStatsReport[now.remoteCandidateId];
  476. this.conferenceStats.transport.push({
  477. ip: `${remote.ipAddress}:${remote.portNumber}`,
  478. type: local.transport,
  479. localip: `${local.ipAddress}:${local.portNumber}`,
  480. p2p: this.peerconnection.isP2P,
  481. localCandidateType: local.candidateType,
  482. remoteCandidateType: remote.candidateType
  483. });
  484. }
  485. if (now.type !== 'ssrc' && now.type !== 'outboundrtp'
  486. && now.type !== 'inboundrtp' && now.type !== 'track') {
  487. continue;
  488. }
  489. const before = this.previousStatsReport[idx];
  490. let ssrc = this.getNonNegativeStat(now, 'ssrc');
  491. // If type="track", take the first SSRC from ssrcIds.
  492. if (now.type === 'track' && Array.isArray(now.ssrcIds)) {
  493. ssrc = Number(now.ssrcIds[0]);
  494. }
  495. if (!before || !ssrc) {
  496. continue;
  497. }
  498. // isRemote is available only in FF and is ignored in case of chrome
  499. // according to the spec
  500. // https://www.w3.org/TR/webrtc-stats/#dom-rtcrtpstreamstats-isremote
  501. // when isRemote is true indicates that the measurements were done at
  502. // the remote endpoint and reported in an RTCP RR/XR.
  503. // Fixes a problem where we are calculating local stats wrong adding
  504. // the sent bytes to the local download bitrate.
  505. // In new W3 stats spec, type="track" has a remoteSource boolean
  506. // property.
  507. if (now.isRemote === true || now.remoteSource === true) {
  508. continue;
  509. }
  510. let ssrcStats = this.ssrc2stats.get(ssrc);
  511. if (!ssrcStats) {
  512. ssrcStats = new SsrcStats();
  513. this.ssrc2stats.set(ssrc, ssrcStats);
  514. }
  515. let isDownloadStream = true;
  516. let key = 'packetsReceived';
  517. let packetsNow = getStatValue(now, key);
  518. if (typeof packetsNow === 'undefined'
  519. || packetsNow === null || packetsNow === '') {
  520. isDownloadStream = false;
  521. key = 'packetsSent';
  522. packetsNow = getStatValue(now, key);
  523. if (typeof packetsNow === 'undefined' || packetsNow === null) {
  524. logger.warn('No packetsReceived nor packetsSent stat found');
  525. }
  526. }
  527. if (!packetsNow || packetsNow < 0) {
  528. packetsNow = 0;
  529. }
  530. const packetsBefore = this.getNonNegativeStat(before, key);
  531. const packetsDiff = Math.max(0, packetsNow - packetsBefore);
  532. const packetsLostNow
  533. = this.getNonNegativeStat(now, 'packetsLost');
  534. const packetsLostBefore
  535. = this.getNonNegativeStat(before, 'packetsLost');
  536. const packetsLostDiff = Math.max(0, packetsLostNow - packetsLostBefore);
  537. ssrcStats.setLoss({
  538. packetsTotal: packetsDiff + packetsLostDiff,
  539. packetsLost: packetsLostDiff,
  540. isDownloadStream
  541. });
  542. const bytesReceivedNow
  543. = this.getNonNegativeStat(now, 'bytesReceived');
  544. const bytesReceivedBefore
  545. = this.getNonNegativeStat(before, 'bytesReceived');
  546. const bytesReceived
  547. = Math.max(0, bytesReceivedNow - bytesReceivedBefore);
  548. let bytesSent = 0;
  549. // TODO: clean this mess up!
  550. let nowBytesTransmitted = getStatValue(now, 'bytesSent');
  551. if (typeof nowBytesTransmitted === 'number'
  552. || typeof nowBytesTransmitted === 'string') {
  553. nowBytesTransmitted = Number(nowBytesTransmitted);
  554. if (!isNaN(nowBytesTransmitted)) {
  555. byteSentStats[ssrc] = nowBytesTransmitted;
  556. if (nowBytesTransmitted > 0) {
  557. bytesSent = nowBytesTransmitted
  558. - getStatValue(before, 'bytesSent');
  559. }
  560. }
  561. }
  562. bytesSent = Math.max(0, bytesSent);
  563. const timeMs = now.timestamp - before.timestamp;
  564. let bitrateReceivedKbps = 0, bitrateSentKbps = 0;
  565. if (timeMs > 0) {
  566. // TODO is there any reason to round here?
  567. bitrateReceivedKbps = Math.round((bytesReceived * 8) / timeMs);
  568. bitrateSentKbps = Math.round((bytesSent * 8) / timeMs);
  569. }
  570. ssrcStats.addBitrate({
  571. 'download': bitrateReceivedKbps,
  572. 'upload': bitrateSentKbps
  573. });
  574. const resolution = {
  575. height: null,
  576. width: null
  577. };
  578. try {
  579. let height, width;
  580. if ((height = getStatValue(now, 'googFrameHeightReceived'))
  581. && (width = getStatValue(now, 'googFrameWidthReceived'))) {
  582. resolution.height = height;
  583. resolution.width = width;
  584. } else if ((height = getStatValue(now, 'googFrameHeightSent'))
  585. && (width = getStatValue(now, 'googFrameWidthSent'))) {
  586. resolution.height = height;
  587. resolution.width = width;
  588. }
  589. } catch (e) { /* not supported*/ }
  590. // Tries to get frame rate
  591. let frameRate;
  592. try {
  593. frameRate = getStatValue(now, 'googFrameRateReceived')
  594. || getStatValue(now, 'googFrameRateSent') || 0;
  595. } catch (e) {
  596. // if it fails with previous properties(chrome),
  597. // let's try with another one (FF)
  598. try {
  599. frameRate = this.getNonNegativeStat(now, 'framerateMean');
  600. } catch (err) { /* not supported*/ }
  601. }
  602. ssrcStats.setFramerate(Math.round(frameRate || 0));
  603. if (resolution.height && resolution.width) {
  604. ssrcStats.setResolution(resolution);
  605. } else {
  606. ssrcStats.setResolution(null);
  607. }
  608. let codec;
  609. // Try to get the codec for later reporting.
  610. try {
  611. codec = getStatValue(now, 'googCodecName') || '';
  612. } catch (e) { /* not supported*/ }
  613. ssrcStats.setCodec(codec);
  614. }
  615. this.eventEmitter.emit(
  616. StatisticsEvents.BYTE_SENT_STATS, this.peerconnection, byteSentStats);
  617. this._processAndEmitReport();
  618. };
  619. /**
  620. *
  621. */
  622. StatsCollector.prototype._processAndEmitReport = function() {
  623. // process stats
  624. const totalPackets = {
  625. download: 0,
  626. upload: 0
  627. };
  628. const lostPackets = {
  629. download: 0,
  630. upload: 0
  631. };
  632. let bitrateDownload = 0;
  633. let bitrateUpload = 0;
  634. const resolutions = {};
  635. const framerates = {};
  636. const codecs = {};
  637. let audioBitrateDownload = 0;
  638. let audioBitrateUpload = 0;
  639. let audioCodec = '';
  640. let videoBitrateDownload = 0;
  641. let videoBitrateUpload = 0;
  642. let videoCodec = '';
  643. for (const [ ssrc, ssrcStats ] of this.ssrc2stats) {
  644. // process packet loss stats
  645. const loss = ssrcStats.loss;
  646. const type = loss.isDownloadStream ? 'download' : 'upload';
  647. totalPackets[type] += loss.packetsTotal;
  648. lostPackets[type] += loss.packetsLost;
  649. // process bitrate stats
  650. bitrateDownload += ssrcStats.bitrate.download;
  651. bitrateUpload += ssrcStats.bitrate.upload;
  652. // collect resolutions and framerates
  653. const track = this.peerconnection.getTrackBySSRC(ssrc);
  654. if (track) {
  655. if (track.isAudioTrack()) {
  656. audioBitrateDownload += ssrcStats.bitrate.download;
  657. audioBitrateUpload += ssrcStats.bitrate.upload;
  658. audioCodec = ssrcStats.codec;
  659. } else {
  660. videoBitrateDownload += ssrcStats.bitrate.download;
  661. videoBitrateUpload += ssrcStats.bitrate.upload;
  662. videoCodec = ssrcStats.codec;
  663. }
  664. const participantId = track.getParticipantId();
  665. if (participantId) {
  666. const resolution = ssrcStats.resolution;
  667. if (resolution.width
  668. && resolution.height
  669. && resolution.width !== -1
  670. && resolution.height !== -1) {
  671. const userResolutions = resolutions[participantId] || {};
  672. userResolutions[ssrc] = resolution;
  673. resolutions[participantId] = userResolutions;
  674. }
  675. if (ssrcStats.framerate !== 0) {
  676. const userFramerates = framerates[participantId] || {};
  677. userFramerates[ssrc] = ssrcStats.framerate;
  678. framerates[participantId] = userFramerates;
  679. }
  680. if (audioCodec.length && videoCodec.length) {
  681. const codecDesc = {
  682. 'audio': audioCodec,
  683. 'video': videoCodec
  684. };
  685. const userCodecs = codecs[participantId] || {};
  686. userCodecs[ssrc] = codecDesc;
  687. codecs[participantId] = userCodecs;
  688. }
  689. } else {
  690. logger.error(`No participant ID returned by ${track}`);
  691. }
  692. }
  693. ssrcStats.resetBitrate();
  694. }
  695. this.conferenceStats.bitrate = {
  696. 'upload': bitrateUpload,
  697. 'download': bitrateDownload
  698. };
  699. this.conferenceStats.bitrate.audio = {
  700. 'upload': audioBitrateUpload,
  701. 'download': audioBitrateDownload
  702. };
  703. this.conferenceStats.bitrate.video = {
  704. 'upload': videoBitrateUpload,
  705. 'download': videoBitrateDownload
  706. };
  707. this.conferenceStats.packetLoss = {
  708. total:
  709. calculatePacketLoss(
  710. lostPackets.download + lostPackets.upload,
  711. totalPackets.download + totalPackets.upload),
  712. download:
  713. calculatePacketLoss(lostPackets.download, totalPackets.download),
  714. upload:
  715. calculatePacketLoss(lostPackets.upload, totalPackets.upload)
  716. };
  717. const avgAudioLevels = {};
  718. let localAvgAudioLevels;
  719. Object.keys(this.audioLevelReportHistory).forEach(ssrc => {
  720. const { data, isLocal } = this.audioLevelReportHistory[ssrc];
  721. const avgAudioLevel = data.reduce((sum, currentValue) => sum + currentValue) / data.length;
  722. if (isLocal) {
  723. localAvgAudioLevels = avgAudioLevel;
  724. } else {
  725. const track = this.peerconnection.getTrackBySSRC(Number(ssrc));
  726. if (track) {
  727. const participantId = track.getParticipantId();
  728. if (participantId) {
  729. avgAudioLevels[participantId] = avgAudioLevel;
  730. }
  731. }
  732. }
  733. });
  734. this.audioLevelReportHistory = {};
  735. this.eventEmitter.emit(
  736. StatisticsEvents.CONNECTION_STATS,
  737. this.peerconnection,
  738. {
  739. 'bandwidth': this.conferenceStats.bandwidth,
  740. 'bitrate': this.conferenceStats.bitrate,
  741. 'packetLoss': this.conferenceStats.packetLoss,
  742. 'resolution': resolutions,
  743. 'framerate': framerates,
  744. 'codec': codecs,
  745. 'transport': this.conferenceStats.transport,
  746. localAvgAudioLevels,
  747. avgAudioLevels
  748. });
  749. this.conferenceStats.transport = [];
  750. };
  751. /**
  752. * Stats processing logic.
  753. */
  754. StatsCollector.prototype.processAudioLevelReport = function() {
  755. if (!this.baselineAudioLevelsReport) {
  756. return;
  757. }
  758. const getStatValue = this._getStatValue;
  759. for (const idx in this.currentAudioLevelsReport) {
  760. if (!this.currentAudioLevelsReport.hasOwnProperty(idx)) {
  761. continue;
  762. }
  763. const now = this.currentAudioLevelsReport[idx];
  764. if (now.type !== 'ssrc' && now.type !== 'track') {
  765. continue;
  766. }
  767. const before = this.baselineAudioLevelsReport[idx];
  768. let ssrc = this.getNonNegativeStat(now, 'ssrc');
  769. if (!ssrc && Array.isArray(now.ssrcIds)) {
  770. ssrc = Number(now.ssrcIds[0]);
  771. }
  772. if (!before) {
  773. logger.warn(`${ssrc} not enough data`);
  774. continue;
  775. }
  776. if (!ssrc) {
  777. if ((Date.now() - now.timestamp) < 3000) {
  778. logger.warn('No ssrc: ');
  779. }
  780. continue;
  781. }
  782. // Audio level
  783. let audioLevel;
  784. try {
  785. audioLevel
  786. = getStatValue(now, 'audioInputLevel')
  787. || getStatValue(now, 'audioOutputLevel');
  788. } catch (e) { /* not supported*/
  789. logger.warn('Audio Levels are not available in the statistics.');
  790. clearInterval(this.audioLevelsIntervalId);
  791. return;
  792. }
  793. if (audioLevel) {
  794. let isLocal;
  795. // If type="ssrc" (legacy) check whether they are received packets.
  796. if (now.type === 'ssrc') {
  797. isLocal = !getStatValue(now, 'packetsReceived');
  798. // If type="track", check remoteSource boolean property.
  799. } else {
  800. isLocal = !now.remoteSource;
  801. }
  802. // According to the W3C WebRTC Stats spec, audioLevel should be in
  803. // 0..1 range (0 == silence). However browsers don't behave that
  804. // way so we must convert it to 0..1.
  805. // TODO: Can't find specs about what this value really is, but it
  806. // seems to vary between 0 and around 32k.
  807. audioLevel = audioLevel / 32767;
  808. if (!(ssrc in this.audioLevelReportHistory)) {
  809. this.audioLevelReportHistory[ssrc] = {
  810. isLocal,
  811. data: []
  812. };
  813. }
  814. this.audioLevelReportHistory[ssrc].data.push(audioLevel);
  815. this.eventEmitter.emit(
  816. StatisticsEvents.AUDIO_LEVEL,
  817. this.peerconnection,
  818. ssrc,
  819. audioLevel,
  820. isLocal);
  821. }
  822. }
  823. };
  824. /* eslint-enable no-continue */
  825. /**
  826. * New promised based getStats report processing.
  827. * Tested with chrome, firefox and safari. Not switching it on for chrome as
  828. * frameRate stat is missing and calculating it using framesSent,
  829. * gives values double the values seen in webrtc-internals.
  830. * https://w3c.github.io/webrtc-stats/
  831. */
  832. /**
  833. * Defines a function which (1) is to be used as a StatsCollector method and (2)
  834. * gets the value from a specific report returned by RTCPeerConnection#getStats
  835. * associated with a lib-jitsi-meet browser-agnostic name in case of using
  836. * Promised based getStats.
  837. *
  838. * @param {Object.<string,string>} keys the map of LibJitsi browser-agnostic
  839. * names to RTCPeerConnection#getStats browser-specific keys
  840. */
  841. StatsCollector.prototype._defineNewGetStatValueMethod = function(keys) {
  842. // Define the function which converts a lib-jitsi-meet browser-asnostic name
  843. // to a browser-specific key of a report returned by
  844. // RTCPeerConnection#getStats.
  845. const keyFromName = function(name) {
  846. const key = keys[name];
  847. if (key) {
  848. return key;
  849. }
  850. // eslint-disable-next-line no-throw-literal
  851. throw `The property '${name}' isn't supported!`;
  852. };
  853. // Compose the 2 functions defined above to get a function which retrieves
  854. // the value from a specific report returned by RTCPeerConnection#getStats
  855. // associated with a specific lib-jitsi-meet browser-agnostic name.
  856. return (item, name) => item[keyFromName(name)];
  857. };
  858. /**
  859. * Converts the value to a non-negative number.
  860. * If the value is either invalid or negative then 0 will be returned.
  861. * @param {*} v
  862. * @return {number}
  863. * @private
  864. */
  865. StatsCollector.prototype.getNonNegativeValue = function(v) {
  866. let value = v;
  867. if (typeof value !== 'number') {
  868. value = Number(value);
  869. }
  870. if (isNaN(value)) {
  871. return 0;
  872. }
  873. return Math.max(0, value);
  874. };
  875. /**
  876. * Calculates bitrate between before and now using a supplied field name and its
  877. * value in the stats.
  878. * @param {RTCInboundRtpStreamStats|RTCSentRtpStreamStats} now the current stats
  879. * @param {RTCInboundRtpStreamStats|RTCSentRtpStreamStats} before the
  880. * previous stats.
  881. * @param fieldName the field to use for calculations.
  882. * @return {number} the calculated bitrate between now and before.
  883. * @private
  884. */
  885. StatsCollector.prototype._calculateBitrate = function(now, before, fieldName) {
  886. const bytesNow = this.getNonNegativeValue(now[fieldName]);
  887. const bytesBefore = this.getNonNegativeValue(before[fieldName]);
  888. const bytesProcessed = Math.max(0, bytesNow - bytesBefore);
  889. const timeMs = now.timestamp - before.timestamp;
  890. let bitrateKbps = 0;
  891. if (timeMs > 0) {
  892. // TODO is there any reason to round here?
  893. bitrateKbps = Math.round((bytesProcessed * 8) / timeMs);
  894. }
  895. return bitrateKbps;
  896. };
  897. /**
  898. * Stats processing new getStats logic.
  899. */
  900. StatsCollector.prototype.processNewStatsReport = function() {
  901. if (!this.previousStatsReport) {
  902. return;
  903. }
  904. const getStatValue = this._getStatValue;
  905. const byteSentStats = {};
  906. this.currentStatsReport.forEach(now => {
  907. // RTCIceCandidatePairStats
  908. // https://w3c.github.io/webrtc-stats/#candidatepair-dict*
  909. if (now.type === 'candidate-pair'
  910. && now.nominated
  911. && now.state === 'succeeded') {
  912. const availableIncomingBitrate = now.availableIncomingBitrate;
  913. const availableOutgoingBitrate = now.availableOutgoingBitrate;
  914. if (availableIncomingBitrate || availableOutgoingBitrate) {
  915. this.conferenceStats.bandwidth = {
  916. 'download': Math.round(availableIncomingBitrate / 1000),
  917. 'upload': Math.round(availableOutgoingBitrate / 1000)
  918. };
  919. }
  920. const remoteUsedCandidate
  921. = this.currentStatsReport.get(now.remoteCandidateId);
  922. const localUsedCandidate
  923. = this.currentStatsReport.get(now.localCandidateId);
  924. // RTCIceCandidateStats
  925. // https://w3c.github.io/webrtc-stats/#icecandidate-dict*
  926. // safari currently does not provide ice candidates in stats
  927. if (remoteUsedCandidate && localUsedCandidate) {
  928. const remoteIpAddress = getStatValue(remoteUsedCandidate, 'ip');
  929. const remotePort = getStatValue(remoteUsedCandidate, 'port');
  930. const ip = `${remoteIpAddress}:${remotePort}`;
  931. const localIpAddress = getStatValue(localUsedCandidate, 'ip');
  932. const localPort = getStatValue(localUsedCandidate, 'port');
  933. const localIp = `${localIpAddress}:${localPort}`;
  934. const type = getStatValue(remoteUsedCandidate, 'protocol');
  935. // Save the address unless it has been saved already.
  936. const conferenceStatsTransport = this.conferenceStats.transport;
  937. if (!conferenceStatsTransport.some(
  938. t =>
  939. t.ip === ip
  940. && t.type === type
  941. && t.localip === localIp)) {
  942. conferenceStatsTransport.push({
  943. ip,
  944. type,
  945. localIp,
  946. p2p: this.peerconnection.isP2P,
  947. localCandidateType: localUsedCandidate.candidateType,
  948. remoteCandidateType: remoteUsedCandidate.candidateType,
  949. networkType: localUsedCandidate.networkType,
  950. rtt: now.currentRoundTripTime * 1000
  951. });
  952. }
  953. }
  954. // RTCReceivedRtpStreamStats
  955. // https://w3c.github.io/webrtc-stats/#receivedrtpstats-dict*
  956. // RTCSentRtpStreamStats
  957. // https://w3c.github.io/webrtc-stats/#sentrtpstats-dict*
  958. } else if (now.type === 'inbound-rtp' || now.type === 'outbound-rtp') {
  959. const before = this.previousStatsReport.get(now.id);
  960. const ssrc = this.getNonNegativeValue(now.ssrc);
  961. if (!before || !ssrc) {
  962. return;
  963. }
  964. let ssrcStats = this.ssrc2stats.get(ssrc);
  965. if (!ssrcStats) {
  966. ssrcStats = new SsrcStats();
  967. this.ssrc2stats.set(ssrc, ssrcStats);
  968. }
  969. let isDownloadStream = true;
  970. let key = 'packetsReceived';
  971. if (now.type === 'outbound-rtp') {
  972. isDownloadStream = false;
  973. key = 'packetsSent';
  974. }
  975. let packetsNow = now[key];
  976. if (!packetsNow || packetsNow < 0) {
  977. packetsNow = 0;
  978. }
  979. const packetsBefore = this.getNonNegativeValue(before[key]);
  980. const packetsDiff = Math.max(0, packetsNow - packetsBefore);
  981. const packetsLostNow
  982. = this.getNonNegativeValue(now.packetsLost);
  983. const packetsLostBefore
  984. = this.getNonNegativeValue(before.packetsLost);
  985. const packetsLostDiff
  986. = Math.max(0, packetsLostNow - packetsLostBefore);
  987. ssrcStats.setLoss({
  988. packetsTotal: packetsDiff + packetsLostDiff,
  989. packetsLost: packetsLostDiff,
  990. isDownloadStream
  991. });
  992. if (now.type === 'inbound-rtp') {
  993. ssrcStats.addBitrate({
  994. 'download': this._calculateBitrate(
  995. now, before, 'bytesReceived'),
  996. 'upload': 0
  997. });
  998. // RTCInboundRtpStreamStats
  999. // https://w3c.github.io/webrtc-stats/#inboundrtpstats-dict*
  1000. // TODO: can we use framesDecoded for frame rate, available
  1001. // in chrome
  1002. } else {
  1003. byteSentStats[ssrc] = this.getNonNegativeValue(now.bytesSent);
  1004. ssrcStats.addBitrate({
  1005. 'download': 0,
  1006. 'upload': this._calculateBitrate(
  1007. now, before, 'bytesSent')
  1008. });
  1009. // RTCOutboundRtpStreamStats
  1010. // https://w3c.github.io/webrtc-stats/#outboundrtpstats-dict*
  1011. // TODO: can we use framesEncoded for frame rate, available
  1012. // in chrome
  1013. }
  1014. // FF has framerateMean out of spec
  1015. const framerateMean = now.framerateMean;
  1016. if (framerateMean) {
  1017. ssrcStats.setFramerate(Math.round(framerateMean || 0));
  1018. }
  1019. // track for resolution
  1020. // RTCVideoHandlerStats
  1021. // https://w3c.github.io/webrtc-stats/#vststats-dict*
  1022. // RTCMediaHandlerStats
  1023. // https://w3c.github.io/webrtc-stats/#mststats-dict*
  1024. } else if (now.type === 'track') {
  1025. const resolution = {
  1026. height: now.frameHeight,
  1027. width: now.frameWidth
  1028. };
  1029. // Tries to get frame rate
  1030. let frameRate = now.framesPerSecond;
  1031. if (!frameRate) {
  1032. // we need to calculate it
  1033. const before = this.previousStatsReport.get(now.id);
  1034. if (before) {
  1035. const timeMs = now.timestamp - before.timestamp;
  1036. if (timeMs > 0 && now.framesSent) {
  1037. const numberOfFramesSinceBefore
  1038. = now.framesSent - before.framesSent;
  1039. frameRate = (numberOfFramesSinceBefore / timeMs) * 1000;
  1040. }
  1041. }
  1042. if (!frameRate) {
  1043. return;
  1044. }
  1045. }
  1046. const trackIdentifier = now.trackIdentifier;
  1047. const ssrc = this.peerconnection.getSsrcByTrackId(trackIdentifier);
  1048. if (!ssrc) {
  1049. return;
  1050. }
  1051. let ssrcStats = this.ssrc2stats.get(ssrc);
  1052. if (!ssrcStats) {
  1053. ssrcStats = new SsrcStats();
  1054. this.ssrc2stats.set(ssrc, ssrcStats);
  1055. }
  1056. ssrcStats.setFramerate(Math.round(frameRate || 0));
  1057. if (resolution.height && resolution.width) {
  1058. ssrcStats.setResolution(resolution);
  1059. } else {
  1060. ssrcStats.setResolution(null);
  1061. }
  1062. }
  1063. });
  1064. this.eventEmitter.emit(
  1065. StatisticsEvents.BYTE_SENT_STATS, this.peerconnection, byteSentStats);
  1066. this._processAndEmitReport();
  1067. };
  1068. /**
  1069. * Stats processing logic.
  1070. */
  1071. StatsCollector.prototype.processNewAudioLevelReport = function() {
  1072. if (!this.baselineAudioLevelsReport) {
  1073. return;
  1074. }
  1075. this.currentAudioLevelsReport.forEach(now => {
  1076. if (now.type !== 'track') {
  1077. return;
  1078. }
  1079. // Audio level
  1080. const audioLevel = now.audioLevel;
  1081. if (!audioLevel) {
  1082. return;
  1083. }
  1084. const trackIdentifier = now.trackIdentifier;
  1085. const ssrc = this.peerconnection.getSsrcByTrackId(trackIdentifier);
  1086. if (ssrc) {
  1087. const isLocal
  1088. = ssrc === this.peerconnection.getLocalSSRC(
  1089. this.peerconnection.getLocalTracks(MediaType.AUDIO));
  1090. this.eventEmitter.emit(
  1091. StatisticsEvents.AUDIO_LEVEL,
  1092. this.peerconnection,
  1093. ssrc,
  1094. audioLevel,
  1095. isLocal);
  1096. }
  1097. });
  1098. };
  1099. /**
  1100. * End new promised based getStats processing methods.
  1101. */