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 23KB

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