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

RTPStatsCollector.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /* global require */
  2. /* jshint -W101 */
  3. var logger = require("jitsi-meet-logger").getLogger(__filename);
  4. var RTCBrowserType = require("../RTC/RTCBrowserType");
  5. var StatisticsEvents = require("../../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. var keyMap = {};
  10. keyMap[RTCBrowserType.RTC_BROWSER_FIREFOX] = {
  11. "ssrc": "ssrc",
  12. "packetsReceived": "packetsReceived",
  13. "packetsLost": "packetsLost",
  14. "packetsSent": "packetsSent",
  15. "bytesReceived": "bytesReceived",
  16. "bytesSent": "bytesSent"
  17. };
  18. keyMap[RTCBrowserType.RTC_BROWSER_CHROME] = {
  19. "receiveBandwidth": "googAvailableReceiveBandwidth",
  20. "sendBandwidth": "googAvailableSendBandwidth",
  21. "remoteAddress": "googRemoteAddress",
  22. "transportType": "googTransportType",
  23. "localAddress": "googLocalAddress",
  24. "activeConnection": "googActiveConnection",
  25. "ssrc": "ssrc",
  26. "packetsReceived": "packetsReceived",
  27. "packetsSent": "packetsSent",
  28. "packetsLost": "packetsLost",
  29. "bytesReceived": "bytesReceived",
  30. "bytesSent": "bytesSent",
  31. "googFrameHeightReceived": "googFrameHeightReceived",
  32. "googFrameWidthReceived": "googFrameWidthReceived",
  33. "googFrameHeightSent": "googFrameHeightSent",
  34. "googFrameWidthSent": "googFrameWidthSent",
  35. "audioInputLevel": "audioInputLevel",
  36. "audioOutputLevel": "audioOutputLevel"
  37. };
  38. keyMap[RTCBrowserType.RTC_BROWSER_OPERA] =
  39. keyMap[RTCBrowserType.RTC_BROWSER_CHROME];
  40. keyMap[RTCBrowserType.RTC_BROWSER_IEXPLORER] =
  41. keyMap[RTCBrowserType.RTC_BROWSER_CHROME];
  42. keyMap[RTCBrowserType.RTC_BROWSER_SAFARI] =
  43. keyMap[RTCBrowserType.RTC_BROWSER_CHROME];
  44. /**
  45. * Calculates packet lost percent using the number of lost packets and the
  46. * number of all packet.
  47. * @param lostPackets the number of lost packets
  48. * @param totalPackets the number of all packets.
  49. * @returns {number} packet loss percent
  50. */
  51. function calculatePacketLoss(lostPackets, totalPackets) {
  52. if(!totalPackets || totalPackets <= 0 || !lostPackets || lostPackets <= 0)
  53. return 0;
  54. return Math.round((lostPackets/totalPackets)*100);
  55. }
  56. function getStatValue(item, name) {
  57. var browserType = RTCBrowserType.getBrowserType();
  58. if (!keyMap[browserType][name])
  59. throw "The property isn't supported!";
  60. var key = keyMap[browserType][name];
  61. return (RTCBrowserType.isChrome() || RTCBrowserType.isOpera()) ?
  62. item.stat(key) : item[key];
  63. }
  64. function formatAudioLevel(audioLevel) {
  65. return Math.min(Math.max(audioLevel, 0), 1);
  66. }
  67. /**
  68. * Checks whether a certain record should be included in the logged statistics.
  69. */
  70. function acceptStat(reportId, reportType, statName) {
  71. if (reportType == "googCandidatePair" && statName == "googChannelId")
  72. return false;
  73. if (reportType == "ssrc") {
  74. if (statName == "googTrackId" ||
  75. statName == "transportId" ||
  76. statName == "ssrc")
  77. return false;
  78. }
  79. return true;
  80. }
  81. /**
  82. * Checks whether a certain record should be included in the logged statistics.
  83. */
  84. function acceptReport(id, type) {
  85. if (id.substring(0, 15) == "googCertificate" ||
  86. id.substring(0, 9) == "googTrack" ||
  87. id.substring(0, 20) == "googLibjingleSession")
  88. return false;
  89. if (type == "googComponent")
  90. return false;
  91. return true;
  92. }
  93. /**
  94. * Peer statistics data holder.
  95. * @constructor
  96. */
  97. function PeerStats() {
  98. this.ssrc2Loss = {};
  99. this.ssrc2AudioLevel = {};
  100. this.ssrc2bitrate = {
  101. download: 0,
  102. upload: 0
  103. };
  104. this.ssrc2resolution = {};
  105. }
  106. /**
  107. * Sets packets loss rate for given <tt>ssrc</tt> that belong to the peer
  108. * represented by this instance.
  109. * @param lossRate new packet loss rate value to be set.
  110. */
  111. PeerStats.prototype.setSsrcLoss = function (lossRate) {
  112. this.ssrc2Loss = lossRate || {};
  113. };
  114. /**
  115. * Sets resolution that belong to the ssrc
  116. * represented by this instance.
  117. * @param resolution new resolution value to be set.
  118. */
  119. PeerStats.prototype.setSsrcResolution = function (resolution) {
  120. this.ssrc2resolution = resolution || {};
  121. };
  122. /**
  123. * Sets the bit rate for given <tt>ssrc</tt> that belong to the peer
  124. * represented by this instance.
  125. * @param bitrate new bitrate value to be set.
  126. */
  127. PeerStats.prototype.setSsrcBitrate = function (bitrate) {
  128. this.ssrc2bitrate.download += bitrate.download;
  129. this.ssrc2bitrate.upload += bitrate.upload;
  130. };
  131. /**
  132. * Resets the bit rate for given <tt>ssrc</tt> that belong to the peer
  133. * represented by this instance.
  134. */
  135. PeerStats.prototype.resetSsrcBitrate = function () {
  136. this.ssrc2bitrate.download = 0;
  137. this.ssrc2bitrate.upload = 0;
  138. };
  139. /**
  140. * Sets new audio level(input or output) for given <tt>ssrc</tt> that identifies
  141. * the stream which belongs to the peer represented by this instance.
  142. * @param audioLevel the new audio level value to be set. Value is truncated to
  143. * fit the range from 0 to 1.
  144. */
  145. PeerStats.prototype.setSsrcAudioLevel = function (audioLevel) {
  146. // Range limit 0 - 1
  147. this.ssrc2AudioLevel = formatAudioLevel(audioLevel);
  148. };
  149. function ConferenceStats() {
  150. /**
  151. * The bandwidth
  152. * @type {{}}
  153. */
  154. this.bandwidth = {};
  155. /**
  156. * The bit rate
  157. * @type {{}}
  158. */
  159. this.bitrate = {};
  160. /**
  161. * The packet loss rate
  162. * @type {{}}
  163. */
  164. this.packetLoss = null;
  165. /**
  166. * Array with the transport information.
  167. * @type {Array}
  168. */
  169. this.transport = [];
  170. }
  171. /**
  172. * <tt>StatsCollector</tt> registers for stats updates of given
  173. * <tt>peerconnection</tt> in given <tt>interval</tt>. On each update particular
  174. * stats are extracted and put in {@link PeerStats} objects. Once the processing
  175. * is done <tt>audioLevelsUpdateCallback</tt> is called with <tt>this</tt>
  176. * instance as an event source.
  177. *
  178. * @param peerconnection webRTC peer connection object.
  179. * @param interval stats refresh interval given in ms.
  180. * @param {function(StatsCollector)} audioLevelsUpdateCallback the callback
  181. * called on stats update.
  182. * @param config {object} supports the following properties - disableAudioLevels, disableStats, logStats
  183. * @constructor
  184. */
  185. function StatsCollector(peerconnection, audioLevelsInterval, statsInterval, eventEmitter, config)
  186. {
  187. this.peerconnection = peerconnection;
  188. this.baselineAudioLevelsReport = null;
  189. this.currentAudioLevelsReport = null;
  190. this.currentStatsReport = null;
  191. this.baselineStatsReport = null;
  192. this.audioLevelsIntervalId = null;
  193. this.eventEmitter = eventEmitter;
  194. this.config = config || {};
  195. this.conferenceStats = new ConferenceStats();
  196. /**
  197. * Gather PeerConnection stats once every this many milliseconds.
  198. */
  199. this.GATHER_INTERVAL = 15000;
  200. /**
  201. * Gather stats and store them in this.statsToBeLogged.
  202. */
  203. this.gatherStatsIntervalId = null;
  204. /**
  205. * Stores the statistics which will be send to the focus to be logged.
  206. */
  207. this.statsToBeLogged = {
  208. timestamps: [],
  209. stats: {}
  210. };
  211. // Updates stats interval
  212. this.audioLevelsIntervalMilis = audioLevelsInterval;
  213. this.statsIntervalId = null;
  214. this.statsIntervalMilis = statsInterval;
  215. // Map of ssrcs to PeerStats
  216. this.ssrc2stats = {};
  217. }
  218. module.exports = StatsCollector;
  219. /**
  220. * Stops stats updates.
  221. */
  222. StatsCollector.prototype.stop = function () {
  223. if (this.audioLevelsIntervalId) {
  224. clearInterval(this.audioLevelsIntervalId);
  225. this.audioLevelsIntervalId = null;
  226. }
  227. if (this.statsIntervalId) {
  228. clearInterval(this.statsIntervalId);
  229. this.statsIntervalId = null;
  230. }
  231. if (this.gatherStatsIntervalId) {
  232. clearInterval(this.gatherStatsIntervalId);
  233. this.gatherStatsIntervalId = null;
  234. }
  235. };
  236. /**
  237. * Callback passed to <tt>getStats</tt> method.
  238. * @param error an error that occurred on <tt>getStats</tt> call.
  239. */
  240. StatsCollector.prototype.errorCallback = function (error) {
  241. logger.error("Get stats error", error);
  242. this.stop();
  243. };
  244. /**
  245. * Starts stats updates.
  246. */
  247. StatsCollector.prototype.start = function () {
  248. var self = this;
  249. this.audioLevelsIntervalId = setInterval(
  250. function () {
  251. // Interval updates
  252. self.peerconnection.getStats(
  253. function (report) {
  254. var results = null;
  255. if (!report || !report.result ||
  256. typeof report.result != 'function') {
  257. results = report;
  258. }
  259. else {
  260. results = report.result();
  261. }
  262. //logger.error("Got interval report", results);
  263. self.currentAudioLevelsReport = results;
  264. self.processAudioLevelReport();
  265. self.baselineAudioLevelsReport =
  266. self.currentAudioLevelsReport;
  267. },
  268. self.errorCallback
  269. );
  270. },
  271. self.audioLevelsIntervalMilis
  272. );
  273. if (!this.config.disableStats && browserSupported) {
  274. this.statsIntervalId = setInterval(
  275. function () {
  276. // Interval updates
  277. self.peerconnection.getStats(
  278. function (report) {
  279. var results = null;
  280. if (!report || !report.result ||
  281. typeof report.result != 'function') {
  282. //firefox
  283. results = report;
  284. }
  285. else {
  286. //chrome
  287. results = report.result();
  288. }
  289. //logger.error("Got interval report", results);
  290. self.currentStatsReport = results;
  291. try {
  292. self.processStatsReport();
  293. }
  294. catch (e) {
  295. logger.error("Unsupported key:" + e, e);
  296. }
  297. self.baselineStatsReport = self.currentStatsReport;
  298. },
  299. self.errorCallback
  300. );
  301. },
  302. self.statsIntervalMilis
  303. );
  304. }
  305. // logging statistics does not support firefox
  306. if (this.config.logStats && (browserSupported && !RTCBrowserType.isFirefox())) {
  307. this.gatherStatsIntervalId = setInterval(
  308. function () {
  309. self.peerconnection.getStats(
  310. function (report) {
  311. self.addStatsToBeLogged(report.result());
  312. },
  313. function () {
  314. }
  315. );
  316. },
  317. this.GATHER_INTERVAL
  318. );
  319. }
  320. };
  321. /**
  322. * Converts the stats to the format used for logging, and saves the data in
  323. * this.statsToBeLogged.
  324. * @param reports Reports as given by webkitRTCPerConnection.getStats.
  325. */
  326. StatsCollector.prototype.addStatsToBeLogged = function (reports) {
  327. var self = this;
  328. var num_records = this.statsToBeLogged.timestamps.length;
  329. this.statsToBeLogged.timestamps.push(new Date().getTime());
  330. reports.forEach(function (report) {
  331. if (!acceptReport(report.id, report.type))
  332. return;
  333. var stat = self.statsToBeLogged.stats[report.id];
  334. if (!stat) {
  335. stat = self.statsToBeLogged.stats[report.id] = {};
  336. }
  337. stat.type = report.type;
  338. report.names().forEach(function (name) {
  339. if (!acceptStat(report.id, report.type, name))
  340. return;
  341. var values = stat[name];
  342. if (!values) {
  343. values = stat[name] = [];
  344. }
  345. while (values.length < num_records) {
  346. values.push(null);
  347. }
  348. values.push(report.stat(name));
  349. });
  350. });
  351. };
  352. StatsCollector.prototype.getCollectedStats = function () {
  353. return this.statsToBeLogged;
  354. };
  355. StatsCollector.prototype.clearCollectedStats = function () {
  356. // Reset the stats
  357. this.statsToBeLogged.stats = {};
  358. this.statsToBeLogged.timestamps = [];
  359. };
  360. /**
  361. * Stats processing logic.
  362. */
  363. StatsCollector.prototype.processStatsReport = function () {
  364. if (!this.baselineStatsReport) {
  365. return;
  366. }
  367. for (var idx in this.currentStatsReport) {
  368. var now = this.currentStatsReport[idx];
  369. try {
  370. if (getStatValue(now, 'receiveBandwidth') ||
  371. getStatValue(now, 'sendBandwidth')) {
  372. this.conferenceStats.bandwidth = {
  373. "download": Math.round(
  374. (getStatValue(now, 'receiveBandwidth')) / 1000),
  375. "upload": Math.round(
  376. (getStatValue(now, 'sendBandwidth')) / 1000)
  377. };
  378. }
  379. }
  380. catch(e){/*not supported*/}
  381. if(now.type == 'googCandidatePair')
  382. {
  383. var ip, type, localIP, active;
  384. try {
  385. ip = getStatValue(now, 'remoteAddress');
  386. type = getStatValue(now, "transportType");
  387. localIP = getStatValue(now, "localAddress");
  388. active = getStatValue(now, "activeConnection");
  389. }
  390. catch(e){/*not supported*/}
  391. if(!ip || !type || !localIP || active != "true")
  392. continue;
  393. var addressSaved = false;
  394. for(var i = 0; i < this.conferenceStats.transport.length; i++)
  395. {
  396. if(this.conferenceStats.transport[i].ip == ip &&
  397. this.conferenceStats.transport[i].type == type &&
  398. this.conferenceStats.transport[i].localip == localIP)
  399. {
  400. addressSaved = true;
  401. }
  402. }
  403. if(addressSaved)
  404. continue;
  405. this.conferenceStats.transport.push({localip: localIP, ip: ip, type: type});
  406. continue;
  407. }
  408. if(now.type == "candidatepair") {
  409. if(now.state == "succeeded")
  410. continue;
  411. var local = this.currentStatsReport[now.localCandidateId];
  412. var remote = this.currentStatsReport[now.remoteCandidateId];
  413. this.conferenceStats.transport.push({
  414. localip: local.ipAddress + ":" + local.portNumber,
  415. ip: remote.ipAddress + ":" + remote.portNumber,
  416. type: local.transport
  417. });
  418. }
  419. if (now.type != 'ssrc' && now.type != "outboundrtp" &&
  420. now.type != "inboundrtp") {
  421. continue;
  422. }
  423. var before = this.baselineStatsReport[idx];
  424. if (!before) {
  425. logger.warn(getStatValue(now, 'ssrc') + ' not enough data');
  426. continue;
  427. }
  428. var ssrc = getStatValue(now, 'ssrc');
  429. if(!ssrc)
  430. continue;
  431. var ssrcStats = this.ssrc2stats[ssrc];
  432. if (!ssrcStats) {
  433. ssrcStats = new PeerStats();
  434. this.ssrc2stats[ssrc] = ssrcStats;
  435. }
  436. var isDownloadStream = true;
  437. var key = 'packetsReceived';
  438. var packetsNow = getStatValue(now, key);
  439. if (typeof packetsNow === 'undefined'
  440. || packetsNow === null || packetsNow === "") {
  441. isDownloadStream = false;
  442. key = 'packetsSent';
  443. packetsNow = getStatValue(now, key);
  444. if (typeof packetsNow === 'undefined' || packetsNow === null) {
  445. logger.warn("No packetsReceived nor packetsSent stat found");
  446. continue;
  447. }
  448. }
  449. if (!packetsNow || packetsNow < 0)
  450. packetsNow = 0;
  451. var packetsBefore = getStatValue(before, key);
  452. if (!packetsBefore || packetsBefore < 0)
  453. packetsBefore = 0;
  454. var packetRate = packetsNow - packetsBefore;
  455. if (!packetRate || packetRate < 0)
  456. packetRate = 0;
  457. var currentLoss = getStatValue(now, 'packetsLost');
  458. if (!currentLoss || currentLoss < 0)
  459. currentLoss = 0;
  460. var previousLoss = getStatValue(before, 'packetsLost');
  461. if (!previousLoss || previousLoss < 0)
  462. previousLoss = 0;
  463. var lossRate = currentLoss - previousLoss;
  464. if (!lossRate || lossRate < 0)
  465. lossRate = 0;
  466. var packetsTotal = (packetRate + lossRate);
  467. ssrcStats.setSsrcLoss({
  468. packetsTotal: packetsTotal,
  469. packetsLost: lossRate,
  470. isDownloadStream: isDownloadStream
  471. });
  472. var bytesReceived = 0, bytesSent = 0;
  473. if(getStatValue(now, "bytesReceived")) {
  474. bytesReceived = getStatValue(now, "bytesReceived") -
  475. getStatValue(before, "bytesReceived");
  476. }
  477. if (getStatValue(now, "bytesSent")) {
  478. bytesSent = getStatValue(now, "bytesSent") -
  479. getStatValue(before, "bytesSent");
  480. }
  481. var time = Math.round((now.timestamp - before.timestamp) / 1000);
  482. if (bytesReceived <= 0 || time <= 0) {
  483. bytesReceived = 0;
  484. } else {
  485. bytesReceived = Math.round(((bytesReceived * 8) / time) / 1000);
  486. }
  487. if (bytesSent <= 0 || time <= 0) {
  488. bytesSent = 0;
  489. } else {
  490. bytesSent = Math.round(((bytesSent * 8) / time) / 1000);
  491. }
  492. ssrcStats.setSsrcBitrate({
  493. "download": bytesReceived,
  494. "upload": bytesSent
  495. });
  496. var resolution = {height: null, width: null};
  497. try {
  498. if (getStatValue(now, "googFrameHeightReceived") &&
  499. getStatValue(now, "googFrameWidthReceived")) {
  500. resolution.height =
  501. getStatValue(now, "googFrameHeightReceived");
  502. resolution.width = getStatValue(now, "googFrameWidthReceived");
  503. }
  504. else if (getStatValue(now, "googFrameHeightSent") &&
  505. getStatValue(now, "googFrameWidthSent")) {
  506. resolution.height = getStatValue(now, "googFrameHeightSent");
  507. resolution.width = getStatValue(now, "googFrameWidthSent");
  508. }
  509. }
  510. catch(e){/*not supported*/}
  511. if (resolution.height && resolution.width) {
  512. ssrcStats.setSsrcResolution(resolution);
  513. } else {
  514. ssrcStats.setSsrcResolution(null);
  515. }
  516. }
  517. // process stats
  518. var totalPackets = {
  519. download: 0,
  520. upload: 0
  521. };
  522. var lostPackets = {
  523. download: 0,
  524. upload: 0
  525. };
  526. var bitrateDownload = 0;
  527. var bitrateUpload = 0;
  528. var resolutions = {};
  529. Object.keys(this.ssrc2stats).forEach(
  530. function (ssrc) {
  531. var ssrcStats = this.ssrc2stats[ssrc];
  532. // process package loss stats
  533. var ssrc2Loss = ssrcStats.ssrc2Loss;
  534. var type = ssrc2Loss.isDownloadStream ? "download" : "upload";
  535. totalPackets[type] += ssrc2Loss.packetsTotal;
  536. lostPackets[type] += ssrc2Loss.packetsLost;
  537. // process bitrate stats
  538. var ssrc2bitrate = ssrcStats.ssrc2bitrate;
  539. bitrateDownload += ssrc2bitrate.download;
  540. bitrateUpload += ssrc2bitrate.upload;
  541. ssrcStats.resetSsrcBitrate();
  542. // collect resolutions
  543. resolutions[ssrc] = ssrcStats.ssrc2resolution;
  544. },
  545. this
  546. );
  547. this.conferenceStats.bitrate
  548. = {"upload": bitrateUpload, "download": bitrateDownload};
  549. this.conferenceStats.packetLoss = {
  550. total:
  551. calculatePacketLoss(lostPackets.download + lostPackets.upload,
  552. totalPackets.download + totalPackets.upload),
  553. download:
  554. calculatePacketLoss(lostPackets.download, totalPackets.download),
  555. upload:
  556. calculatePacketLoss(lostPackets.upload, totalPackets.upload)
  557. };
  558. this.eventEmitter.emit(StatisticsEvents.CONNECTION_STATS, {
  559. "bitrate": this.conferenceStats.bitrate,
  560. "packetLoss": this.conferenceStats.packetLoss,
  561. "bandwidth": this.conferenceStats.bandwidth,
  562. "resolution": resolutions,
  563. "transport": this.conferenceStats.transport
  564. });
  565. this.conferenceStats.transport = [];
  566. };
  567. /**
  568. * Stats processing logic.
  569. */
  570. StatsCollector.prototype.processAudioLevelReport = function () {
  571. if (!this.baselineAudioLevelsReport) {
  572. return;
  573. }
  574. for (var idx in this.currentAudioLevelsReport) {
  575. var now = this.currentAudioLevelsReport[idx];
  576. //if we don't have "packetsReceived" this is local stream
  577. if (now.type != 'ssrc' || !getStatValue(now, 'packetsReceived')) {
  578. continue;
  579. }
  580. var before = this.baselineAudioLevelsReport[idx];
  581. if (!before) {
  582. logger.warn(getStatValue(now, 'ssrc') + ' not enough data');
  583. continue;
  584. }
  585. var ssrc = getStatValue(now, 'ssrc');
  586. if (!ssrc) {
  587. if ((Date.now() - now.timestamp) < 3000)
  588. logger.warn("No ssrc: ");
  589. continue;
  590. }
  591. var ssrcStats = this.ssrc2stats[ssrc];
  592. if (!ssrcStats) {
  593. ssrcStats = new PeerStats();
  594. this.ssrc2stats[ssrc] = ssrcStats;
  595. }
  596. // Audio level
  597. var audioLevel = null;
  598. try {
  599. audioLevel = getStatValue(now, 'audioInputLevel');
  600. if (!audioLevel)
  601. audioLevel = getStatValue(now, 'audioOutputLevel');
  602. }
  603. catch(e) {/*not supported*/
  604. logger.warn("Audio Levels are not available in the statistics.");
  605. clearInterval(this.audioLevelsIntervalId);
  606. return;
  607. }
  608. if (audioLevel) {
  609. // TODO: can't find specs about what this value really is,
  610. // but it seems to vary between 0 and around 32k.
  611. audioLevel = audioLevel / 32767;
  612. ssrcStats.setSsrcAudioLevel(audioLevel);
  613. this.eventEmitter.emit(
  614. StatisticsEvents.AUDIO_LEVEL, ssrc, audioLevel);
  615. }
  616. }
  617. };