modified lib-jitsi-meet dev repo
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

RTPStatsCollector.js 43KB

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