modified lib-jitsi-meet dev repo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RTPStatsCollector.js 41KB

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