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

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