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

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