Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RTPStatsCollector.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /* global require */
  2. var GlobalOnErrorHandler = require("../util/GlobalOnErrorHandler");
  3. var logger = require("jitsi-meet-logger").getLogger(__filename);
  4. var RTCBrowserType = require("../RTC/RTCBrowserType");
  5. import * as StatisticsEvents from "../../service/statistics/Events";
  6. /* Whether we support the browser we are running into for logging statistics */
  7. var browserSupported = RTCBrowserType.isChrome() ||
  8. RTCBrowserType.isOpera() || RTCBrowserType.isFirefox() ||
  9. RTCBrowserType.isNWJS();
  10. /**
  11. * The LibJitsiMeet browser-agnostic names of the browser-specific keys reported
  12. * by RTCPeerConnection#getStats mapped by RTCBrowserType.
  13. */
  14. var KEYS_BY_BROWSER_TYPE = {};
  15. KEYS_BY_BROWSER_TYPE[RTCBrowserType.RTC_BROWSER_FIREFOX] = {
  16. "ssrc": "ssrc",
  17. "packetsReceived": "packetsReceived",
  18. "packetsLost": "packetsLost",
  19. "packetsSent": "packetsSent",
  20. "bytesReceived": "bytesReceived",
  21. "bytesSent": "bytesSent"
  22. };
  23. KEYS_BY_BROWSER_TYPE[RTCBrowserType.RTC_BROWSER_CHROME] = {
  24. "receiveBandwidth": "googAvailableReceiveBandwidth",
  25. "sendBandwidth": "googAvailableSendBandwidth",
  26. "remoteAddress": "googRemoteAddress",
  27. "transportType": "googTransportType",
  28. "localAddress": "googLocalAddress",
  29. "activeConnection": "googActiveConnection",
  30. "ssrc": "ssrc",
  31. "packetsReceived": "packetsReceived",
  32. "packetsSent": "packetsSent",
  33. "packetsLost": "packetsLost",
  34. "bytesReceived": "bytesReceived",
  35. "bytesSent": "bytesSent",
  36. "googFrameHeightReceived": "googFrameHeightReceived",
  37. "googFrameWidthReceived": "googFrameWidthReceived",
  38. "googFrameHeightSent": "googFrameHeightSent",
  39. "googFrameWidthSent": "googFrameWidthSent",
  40. "audioInputLevel": "audioInputLevel",
  41. "audioOutputLevel": "audioOutputLevel"
  42. };
  43. KEYS_BY_BROWSER_TYPE[RTCBrowserType.RTC_BROWSER_OPERA] =
  44. KEYS_BY_BROWSER_TYPE[RTCBrowserType.RTC_BROWSER_CHROME];
  45. KEYS_BY_BROWSER_TYPE[RTCBrowserType.RTC_BROWSER_NWJS] =
  46. KEYS_BY_BROWSER_TYPE[RTCBrowserType.RTC_BROWSER_CHROME];
  47. KEYS_BY_BROWSER_TYPE[RTCBrowserType.RTC_BROWSER_IEXPLORER] =
  48. KEYS_BY_BROWSER_TYPE[RTCBrowserType.RTC_BROWSER_CHROME];
  49. KEYS_BY_BROWSER_TYPE[RTCBrowserType.RTC_BROWSER_SAFARI] =
  50. KEYS_BY_BROWSER_TYPE[RTCBrowserType.RTC_BROWSER_CHROME];
  51. KEYS_BY_BROWSER_TYPE[RTCBrowserType.RTC_BROWSER_REACT_NATIVE] =
  52. KEYS_BY_BROWSER_TYPE[RTCBrowserType.RTC_BROWSER_CHROME];
  53. /**
  54. * Calculates packet lost percent using the number of lost packets and the
  55. * number of all packet.
  56. * @param lostPackets the number of lost packets
  57. * @param totalPackets the number of all packets.
  58. * @returns {number} packet loss percent
  59. */
  60. function calculatePacketLoss(lostPackets, totalPackets) {
  61. if(!totalPackets || totalPackets <= 0 || !lostPackets || lostPackets <= 0)
  62. return 0;
  63. return Math.round((lostPackets/totalPackets)*100);
  64. }
  65. /**
  66. * Holds "statistics" for a single SSRC.
  67. * @constructor
  68. */
  69. function SsrcStats() {
  70. this.loss = {};
  71. this.bitrate = {
  72. download: 0,
  73. upload: 0
  74. };
  75. this.resolution = {};
  76. }
  77. /**
  78. * Sets the "loss" object.
  79. * @param loss the value to set.
  80. */
  81. SsrcStats.prototype.setLoss = function (loss) {
  82. this.loss = loss || {};
  83. };
  84. /**
  85. * Sets resolution that belong to the ssrc represented by this instance.
  86. * @param resolution new resolution value to be set.
  87. */
  88. SsrcStats.prototype.setResolution = function (resolution) {
  89. this.resolution = resolution || {};
  90. };
  91. /**
  92. * Adds the "download" and "upload" fields from the "bitrate" parameter to
  93. * the respective fields of the "bitrate" field of this object.
  94. * @param bitrate an object holding the values to add.
  95. */
  96. SsrcStats.prototype.addBitrate = function (bitrate) {
  97. this.bitrate.download += bitrate.download;
  98. this.bitrate.upload += bitrate.upload;
  99. };
  100. /**
  101. * Resets the bit rate for given <tt>ssrc</tt> that belong to the peer
  102. * represented by this instance.
  103. */
  104. SsrcStats.prototype.resetBitrate = function () {
  105. this.bitrate.download = 0;
  106. this.bitrate.upload = 0;
  107. };
  108. function ConferenceStats() {
  109. /**
  110. * The bandwidth
  111. * @type {{}}
  112. */
  113. this.bandwidth = {};
  114. /**
  115. * The bit rate
  116. * @type {{}}
  117. */
  118. this.bitrate = {};
  119. /**
  120. * The packet loss rate
  121. * @type {{}}
  122. */
  123. this.packetLoss = null;
  124. /**
  125. * Array with the transport information.
  126. * @type {Array}
  127. */
  128. this.transport = [];
  129. }
  130. /**
  131. * <tt>StatsCollector</tt> registers for stats updates of given
  132. * <tt>peerconnection</tt> in given <tt>interval</tt>. On each update particular
  133. * stats are extracted and put in {@link SsrcStats} objects. Once the processing
  134. * is done <tt>audioLevelsUpdateCallback</tt> is called with <tt>this</tt>
  135. * instance as an event source.
  136. *
  137. * @param peerconnection WebRTC PeerConnection object.
  138. * @param audioLevelsInterval
  139. * @param statsInterval stats refresh interval given in ms.
  140. * @param eventEmitter
  141. * @constructor
  142. */
  143. function StatsCollector(
  144. peerconnection,
  145. audioLevelsInterval,
  146. statsInterval,
  147. eventEmitter) {
  148. // StatsCollector depends entirely on the format of the reports returned by
  149. // RTCPeerConnection#getStats. Given that the value of
  150. // RTCBrowserType#getBrowserType() is very unlikely to change at runtime, it
  151. // makes sense to discover whether StatsCollector supports the executing
  152. // browser as soon as possible. Otherwise, (1) getStatValue would have to
  153. // needlessly check a "static" condition multiple times very very often and
  154. // (2) the lack of support for the executing browser would be discovered and
  155. // reported multiple times very very often too late in the execution in some
  156. // totally unrelated callback.
  157. /**
  158. * The RTCBrowserType supported by this StatsCollector. In other words, the
  159. * RTCBrowserType of the browser which initialized this StatsCollector
  160. * instance.
  161. * @private
  162. */
  163. this._browserType = RTCBrowserType.getBrowserType();
  164. var keys = KEYS_BY_BROWSER_TYPE[this._browserType];
  165. if (!keys)
  166. throw "The browser type '" + this._browserType + "' isn't supported!";
  167. /**
  168. * The function which is to be used to retrieve the value associated in a
  169. * report returned by RTCPeerConnection#getStats with a LibJitsiMeet
  170. * browser-agnostic name/key.
  171. * @function
  172. * @private
  173. */
  174. this._getStatValue = this._defineGetStatValueMethod(keys);
  175. this.peerconnection = peerconnection;
  176. this.baselineAudioLevelsReport = null;
  177. this.currentAudioLevelsReport = null;
  178. this.currentStatsReport = null;
  179. this.previousStatsReport = null;
  180. this.audioLevelsIntervalId = null;
  181. this.eventEmitter = eventEmitter;
  182. this.conferenceStats = new ConferenceStats();
  183. // Updates stats interval
  184. this.audioLevelsIntervalMilis = audioLevelsInterval;
  185. this.statsIntervalId = null;
  186. this.statsIntervalMilis = statsInterval;
  187. // Map of ssrcs to SsrcStats
  188. this.ssrc2stats = {};
  189. }
  190. module.exports = StatsCollector;
  191. /**
  192. * Stops stats updates.
  193. */
  194. StatsCollector.prototype.stop = function () {
  195. if (this.audioLevelsIntervalId) {
  196. clearInterval(this.audioLevelsIntervalId);
  197. this.audioLevelsIntervalId = null;
  198. }
  199. if (this.statsIntervalId) {
  200. clearInterval(this.statsIntervalId);
  201. this.statsIntervalId = null;
  202. }
  203. };
  204. /**
  205. * Callback passed to <tt>getStats</tt> method.
  206. * @param error an error that occurred on <tt>getStats</tt> call.
  207. */
  208. StatsCollector.prototype.errorCallback = function (error) {
  209. GlobalOnErrorHandler.callErrorHandler(error);
  210. logger.error("Get stats error", error);
  211. this.stop();
  212. };
  213. /**
  214. * Starts stats updates.
  215. */
  216. StatsCollector.prototype.start = function (startAudioLevelStats) {
  217. var self = this;
  218. if(startAudioLevelStats) {
  219. this.audioLevelsIntervalId = setInterval(
  220. function () {
  221. // Interval updates
  222. self.peerconnection.getStats(
  223. function (report) {
  224. var results = null;
  225. if (!report || !report.result ||
  226. typeof report.result != 'function') {
  227. results = report;
  228. }
  229. else {
  230. results = report.result();
  231. }
  232. self.currentAudioLevelsReport = results;
  233. self.processAudioLevelReport();
  234. self.baselineAudioLevelsReport =
  235. self.currentAudioLevelsReport;
  236. },
  237. self.errorCallback
  238. );
  239. },
  240. self.audioLevelsIntervalMilis
  241. );
  242. }
  243. if (browserSupported) {
  244. this.statsIntervalId = setInterval(
  245. function () {
  246. // Interval updates
  247. self.peerconnection.getStats(
  248. function (report) {
  249. var results = null;
  250. if (!report || !report.result ||
  251. typeof report.result != 'function') {
  252. //firefox
  253. results = report;
  254. }
  255. else {
  256. //chrome
  257. results = report.result();
  258. }
  259. self.currentStatsReport = results;
  260. try {
  261. self.processStatsReport();
  262. }
  263. catch (e) {
  264. GlobalOnErrorHandler.callErrorHandler(e);
  265. logger.error("Unsupported key:" + e, e);
  266. }
  267. self.previousStatsReport = self.currentStatsReport;
  268. },
  269. self.errorCallback
  270. );
  271. },
  272. self.statsIntervalMilis
  273. );
  274. }
  275. };
  276. /**
  277. * Defines a function which (1) is to be used as a StatsCollector method and (2)
  278. * gets the value from a specific report returned by RTCPeerConnection#getStats
  279. * associated with a LibJitsiMeet browser-agnostic name.
  280. *
  281. * @param {Object.<string,string>} keys the map of LibJitsi browser-agnostic
  282. * names to RTCPeerConnection#getStats browser-specific keys
  283. */
  284. StatsCollector.prototype._defineGetStatValueMethod = function (keys) {
  285. // Define the function which converts a LibJitsiMeet browser-asnostic name
  286. // to a browser-specific key of a report returned by
  287. // RTCPeerConnection#getStats.
  288. var keyFromName = function (name) {
  289. var key = keys[name];
  290. if (key)
  291. return key;
  292. else
  293. throw "The property '" + name + "' isn't supported!";
  294. };
  295. // Define the function which retrieves the value from a specific report
  296. // returned by RTCPeerConnection#getStats associated with a given
  297. // browser-specific key.
  298. var itemStatByKey;
  299. switch (this._browserType) {
  300. case RTCBrowserType.RTC_BROWSER_CHROME:
  301. case RTCBrowserType.RTC_BROWSER_OPERA:
  302. case RTCBrowserType.RTC_BROWSER_NWJS:
  303. // TODO What about other types of browser which are based on Chrome such
  304. // as NW.js? Every time we want to support a new type browser we have to
  305. // go and add more conditions (here and in multiple other places).
  306. // Cannot we do a feature detection instead of a browser type check? For
  307. // example, if item has a stat property of type function, then it's very
  308. // likely that whoever defined it wanted you to call it in order to
  309. // retrieve the value associated with a specific key.
  310. itemStatByKey = function (item, key) { return item.stat(key); };
  311. break;
  312. case RTCBrowserType.RTC_BROWSER_REACT_NATIVE:
  313. // The implementation provided by react-native-webrtc follows the
  314. // Objective-C WebRTC API: RTCStatsReport has a values property of type
  315. // Array in which each element is a key-value pair.
  316. itemStatByKey = function (item, key) {
  317. var value;
  318. item.values.some(function (pair) {
  319. if (pair.hasOwnProperty(key)) {
  320. value = pair[key];
  321. return true;
  322. } else {
  323. return false;
  324. }
  325. });
  326. return value;
  327. };
  328. break;
  329. default:
  330. itemStatByKey = function (item, key) { return item[key]; };
  331. }
  332. // Compose the 2 functions defined above to get a function which retrieves
  333. // the value from a specific report returned by RTCPeerConnection#getStats
  334. // associated with a specific LibJitsiMeet browser-agnostic name.
  335. return function (item, name) {
  336. return itemStatByKey(item, keyFromName(name));
  337. };
  338. };
  339. /**
  340. * Stats processing logic.
  341. */
  342. StatsCollector.prototype.processStatsReport = function () {
  343. if (!this.previousStatsReport) {
  344. return;
  345. }
  346. var getStatValue = this._getStatValue;
  347. function getNonNegativeStat(report, name) {
  348. var value = getStatValue(report, name);
  349. if (typeof value !== 'number') {
  350. value = Number(value);
  351. }
  352. if (isNaN(value)) {
  353. return 0;
  354. }
  355. return Math.max(0, value);
  356. }
  357. var byteSentStats = {};
  358. for (var idx in this.currentStatsReport) {
  359. var now = this.currentStatsReport[idx];
  360. try {
  361. var receiveBandwidth = getStatValue(now, 'receiveBandwidth');
  362. var sendBandwidth = getStatValue(now, 'sendBandwidth');
  363. if (receiveBandwidth || sendBandwidth) {
  364. this.conferenceStats.bandwidth = {
  365. "download": Math.round(receiveBandwidth / 1000),
  366. "upload": Math.round(sendBandwidth / 1000)
  367. };
  368. }
  369. }
  370. catch(e){/*not supported*/}
  371. if(now.type == 'googCandidatePair')
  372. {
  373. var ip, type, localip, active;
  374. try {
  375. ip = getStatValue(now, 'remoteAddress');
  376. type = getStatValue(now, "transportType");
  377. localip = getStatValue(now, "localAddress");
  378. active = getStatValue(now, "activeConnection");
  379. }
  380. catch(e){/*not supported*/}
  381. if(!ip || !type || !localip || active != "true")
  382. continue;
  383. // Save the address unless it has been saved already.
  384. var conferenceStatsTransport = this.conferenceStats.transport;
  385. if(!conferenceStatsTransport.some(function (t) { return (
  386. t.ip == ip && t.type == type && t.localip == localip
  387. );})) {
  388. conferenceStatsTransport.push(
  389. {ip: ip, type: type, localip: localip});
  390. }
  391. continue;
  392. }
  393. if(now.type == "candidatepair") {
  394. if(now.state == "succeeded")
  395. continue;
  396. var local = this.currentStatsReport[now.localCandidateId];
  397. var remote = this.currentStatsReport[now.remoteCandidateId];
  398. this.conferenceStats.transport.push({
  399. ip: remote.ipAddress + ":" + remote.portNumber,
  400. type: local.transport,
  401. localip: local.ipAddress + ":" + local.portNumber
  402. });
  403. }
  404. if (now.type != 'ssrc' && now.type != "outboundrtp" &&
  405. now.type != "inboundrtp") {
  406. continue;
  407. }
  408. var before = this.previousStatsReport[idx];
  409. var ssrc = getStatValue(now, 'ssrc');
  410. if (!before || !ssrc) {
  411. continue;
  412. }
  413. var ssrcStats
  414. = this.ssrc2stats[ssrc] || (this.ssrc2stats[ssrc] = new SsrcStats());
  415. var isDownloadStream = true;
  416. var key = 'packetsReceived';
  417. var packetsNow = getStatValue(now, key);
  418. if (typeof packetsNow === 'undefined'
  419. || packetsNow === null || packetsNow === "") {
  420. isDownloadStream = false;
  421. key = 'packetsSent';
  422. packetsNow = getStatValue(now, key);
  423. if (typeof packetsNow === 'undefined' || packetsNow === null) {
  424. logger.warn("No packetsReceived nor packetsSent stat found");
  425. continue;
  426. }
  427. }
  428. if (!packetsNow || packetsNow < 0)
  429. packetsNow = 0;
  430. var packetsBefore = getNonNegativeStat(before, key);
  431. var packetsDiff = Math.max(0, packetsNow - packetsBefore);
  432. var packetsLostNow = getNonNegativeStat(now, 'packetsLost');
  433. var packetsLostBefore = getNonNegativeStat(before, 'packetsLost');
  434. var packetsLostDiff = Math.max(0, packetsLostNow - packetsLostBefore);
  435. ssrcStats.setLoss({
  436. packetsTotal: packetsDiff + packetsLostDiff,
  437. packetsLost: packetsLostDiff,
  438. isDownloadStream: isDownloadStream
  439. });
  440. var bytesReceivedNow = getNonNegativeStat(now, 'bytesReceived');
  441. var bytesReceivedBefore = getNonNegativeStat(before, 'bytesReceived');
  442. var bytesReceived = Math.max(0, bytesReceivedNow - bytesReceivedBefore);
  443. var bytesSent = 0;
  444. // TODO: clean this mess up!
  445. var nowBytesTransmitted = getStatValue(now, "bytesSent");
  446. if(typeof(nowBytesTransmitted) === "number" ||
  447. typeof(nowBytesTransmitted) === "string") {
  448. nowBytesTransmitted = Number(nowBytesTransmitted);
  449. if(!isNaN(nowBytesTransmitted)){
  450. byteSentStats[ssrc] = nowBytesTransmitted;
  451. if (nowBytesTransmitted > 0) {
  452. bytesSent = nowBytesTransmitted -
  453. getStatValue(before, "bytesSent");
  454. }
  455. }
  456. }
  457. bytesSent = Math.max(0, bytesSent);
  458. var timeMs = now.timestamp - before.timestamp;
  459. var bitrateReceivedKbps = 0, bitrateSentKbps = 0;
  460. if (timeMs > 0) {
  461. // TODO is there any reason to round here?
  462. bitrateReceivedKbps = Math.round((bytesReceived * 8) / timeMs);
  463. bitrateSentKbps = Math.round((bytesSent * 8) / timeMs);
  464. }
  465. ssrcStats.addBitrate({
  466. "download": bitrateReceivedKbps,
  467. "upload": bitrateSentKbps
  468. });
  469. var resolution = {height: null, width: null};
  470. try {
  471. var height, width;
  472. if ((height = getStatValue(now, "googFrameHeightReceived")) &&
  473. (width = getStatValue(now, "googFrameWidthReceived"))) {
  474. resolution.height = height;
  475. resolution.width = width;
  476. }
  477. else if ((height = getStatValue(now, "googFrameHeightSent")) &&
  478. (width = getStatValue(now, "googFrameWidthSent"))) {
  479. resolution.height = height;
  480. resolution.width = width;
  481. }
  482. }
  483. catch(e){/*not supported*/}
  484. if (resolution.height && resolution.width) {
  485. ssrcStats.setResolution(resolution);
  486. } else {
  487. ssrcStats.setResolution(null);
  488. }
  489. }
  490. // process stats
  491. var totalPackets = {
  492. download: 0,
  493. upload: 0
  494. };
  495. var lostPackets = {
  496. download: 0,
  497. upload: 0
  498. };
  499. var bitrateDownload = 0;
  500. var bitrateUpload = 0;
  501. var resolutions = {};
  502. Object.keys(this.ssrc2stats).forEach(
  503. function (ssrc) {
  504. var ssrcStats = this.ssrc2stats[ssrc];
  505. // process packet loss stats
  506. var loss = ssrcStats.loss;
  507. var type = loss.isDownloadStream ? "download" : "upload";
  508. totalPackets[type] += loss.packetsTotal;
  509. lostPackets[type] += loss.packetsLost;
  510. // process bitrate stats
  511. bitrateDownload += ssrcStats.bitrate.download;
  512. bitrateUpload += ssrcStats.bitrate.upload;
  513. ssrcStats.resetBitrate();
  514. // collect resolutions
  515. resolutions[ssrc] = ssrcStats.resolution;
  516. },
  517. this
  518. );
  519. this.eventEmitter.emit(StatisticsEvents.BYTE_SENT_STATS, byteSentStats);
  520. this.conferenceStats.bitrate
  521. = {"upload": bitrateUpload, "download": bitrateDownload};
  522. this.conferenceStats.packetLoss = {
  523. total:
  524. calculatePacketLoss(lostPackets.download + lostPackets.upload,
  525. totalPackets.download + totalPackets.upload),
  526. download:
  527. calculatePacketLoss(lostPackets.download, totalPackets.download),
  528. upload:
  529. calculatePacketLoss(lostPackets.upload, totalPackets.upload)
  530. };
  531. this.eventEmitter.emit(StatisticsEvents.CONNECTION_STATS, {
  532. "bandwidth": this.conferenceStats.bandwidth,
  533. "bitrate": this.conferenceStats.bitrate,
  534. "packetLoss": this.conferenceStats.packetLoss,
  535. "resolution": resolutions,
  536. "transport": this.conferenceStats.transport
  537. });
  538. this.conferenceStats.transport = [];
  539. };
  540. /**
  541. * Stats processing logic.
  542. */
  543. StatsCollector.prototype.processAudioLevelReport = function () {
  544. if (!this.baselineAudioLevelsReport) {
  545. return;
  546. }
  547. var getStatValue = this._getStatValue;
  548. for (var idx in this.currentAudioLevelsReport) {
  549. var now = this.currentAudioLevelsReport[idx];
  550. if (now.type != 'ssrc')
  551. continue;
  552. var before = this.baselineAudioLevelsReport[idx];
  553. var ssrc = getStatValue(now, 'ssrc');
  554. if (!before) {
  555. logger.warn(ssrc + ' not enough data');
  556. continue;
  557. }
  558. if (!ssrc) {
  559. if ((Date.now() - now.timestamp) < 3000)
  560. logger.warn("No ssrc: ");
  561. continue;
  562. }
  563. // Audio level
  564. try {
  565. var audioLevel
  566. = getStatValue(now, 'audioInputLevel')
  567. || getStatValue(now, 'audioOutputLevel');
  568. }
  569. catch(e) {/*not supported*/
  570. logger.warn("Audio Levels are not available in the statistics.");
  571. clearInterval(this.audioLevelsIntervalId);
  572. return;
  573. }
  574. if (audioLevel) {
  575. const isLocal = !getStatValue(now, 'packetsReceived');
  576. // TODO: Can't find specs about what this value really is, but it
  577. // seems to vary between 0 and around 32k.
  578. audioLevel = audioLevel / 32767;
  579. this.eventEmitter.emit(
  580. StatisticsEvents.AUDIO_LEVEL, ssrc, audioLevel, isLocal);
  581. }
  582. }
  583. };