Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

RTPStatsCollector.js 43KB

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