選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RTPStatsCollector.js 43KB

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