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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. this.audioLevelsIntervalId = setInterval(
  207. function () {
  208. // Interval updates
  209. self.peerconnection.getStats(
  210. function (report) {
  211. var results = null;
  212. if (!report || !report.result ||
  213. typeof report.result != 'function') {
  214. results = report;
  215. }
  216. else {
  217. results = report.result();
  218. }
  219. //console.error("Got interval report", results);
  220. self.currentAudioLevelsReport = results;
  221. self.processAudioLevelReport();
  222. self.baselineAudioLevelsReport =
  223. self.currentAudioLevelsReport;
  224. },
  225. self.errorCallback
  226. );
  227. },
  228. self.audioLevelsIntervalMilis
  229. );
  230. }
  231. if(!config.disableStats && !navigator.mozGetUserMedia) {
  232. this.statsIntervalId = setInterval(
  233. function () {
  234. // Interval updates
  235. self.peerconnection.getStats(
  236. function (report) {
  237. var results = null;
  238. if (!report || !report.result ||
  239. typeof report.result != 'function') {
  240. //firefox
  241. results = report;
  242. }
  243. else {
  244. //chrome
  245. results = report.result();
  246. }
  247. //console.error("Got interval report", results);
  248. self.currentStatsReport = results;
  249. try {
  250. self.processStatsReport();
  251. }
  252. catch (e) {
  253. console.error("Unsupported key:" + e, e);
  254. }
  255. self.baselineStatsReport = self.currentStatsReport;
  256. },
  257. self.errorCallback
  258. );
  259. },
  260. self.statsIntervalMilis
  261. );
  262. }
  263. if (config.logStats && !navigator.mozGetUserMedia) {
  264. this.gatherStatsIntervalId = setInterval(
  265. function () {
  266. self.peerconnection.getStats(
  267. function (report) {
  268. self.addStatsToBeLogged(report.result());
  269. },
  270. function () {
  271. }
  272. );
  273. },
  274. this.GATHER_INTERVAL
  275. );
  276. this.logStatsIntervalId = setInterval(
  277. function() { self.logStats(); },
  278. this.LOG_INTERVAL);
  279. }
  280. };
  281. /**
  282. * Checks whether a certain record should be included in the logged statistics.
  283. */
  284. function acceptStat(reportId, reportType, statName) {
  285. if (reportType == "googCandidatePair" && statName == "googChannelId")
  286. return false;
  287. if (reportType == "ssrc") {
  288. if (statName == "googTrackId" ||
  289. statName == "transportId" ||
  290. statName == "ssrc")
  291. return false;
  292. }
  293. return true;
  294. }
  295. /**
  296. * Checks whether a certain record should be included in the logged statistics.
  297. */
  298. function acceptReport(id, type) {
  299. if (id.substring(0, 15) == "googCertificate" ||
  300. id.substring(0, 9) == "googTrack" ||
  301. id.substring(0, 20) == "googLibjingleSession")
  302. return false;
  303. if (type == "googComponent")
  304. return false;
  305. return true;
  306. }
  307. /**
  308. * Converts the stats to the format used for logging, and saves the data in
  309. * this.statsToBeLogged.
  310. * @param reports Reports as given by webkitRTCPerConnection.getStats.
  311. */
  312. StatsCollector.prototype.addStatsToBeLogged = function (reports) {
  313. var self = this;
  314. var num_records = this.statsToBeLogged.timestamps.length;
  315. this.statsToBeLogged.timestamps.push(new Date().getTime());
  316. reports.map(function (report) {
  317. if (!acceptReport(report.id, report.type))
  318. return;
  319. var stat = self.statsToBeLogged.stats[report.id];
  320. if (!stat) {
  321. stat = self.statsToBeLogged.stats[report.id] = {};
  322. }
  323. stat.type = report.type;
  324. report.names().map(function (name) {
  325. if (!acceptStat(report.id, report.type, name))
  326. return;
  327. var values = stat[name];
  328. if (!values) {
  329. values = stat[name] = [];
  330. }
  331. while (values.length < num_records) {
  332. values.push(null);
  333. }
  334. values.push(report.stat(name));
  335. });
  336. });
  337. };
  338. StatsCollector.prototype.logStats = function () {
  339. if(!APP.xmpp.sendLogs(this.statsToBeLogged))
  340. return;
  341. // Reset the stats
  342. this.statsToBeLogged.stats = {};
  343. this.statsToBeLogged.timestamps = [];
  344. };
  345. var keyMap = {};
  346. keyMap[RTCBrowserType.RTC_BROWSER_FIREFOX] = {
  347. "ssrc": "ssrc",
  348. "packetsReceived": "packetsReceived",
  349. "packetsLost": "packetsLost",
  350. "packetsSent": "packetsSent",
  351. "bytesReceived": "bytesReceived",
  352. "bytesSent": "bytesSent"
  353. };
  354. keyMap[RTCBrowserType.RTC_BROWSER_CHROME] = {
  355. "receiveBandwidth": "googAvailableReceiveBandwidth",
  356. "sendBandwidth": "googAvailableSendBandwidth",
  357. "remoteAddress": "googRemoteAddress",
  358. "transportType": "googTransportType",
  359. "localAddress": "googLocalAddress",
  360. "activeConnection": "googActiveConnection",
  361. "ssrc": "ssrc",
  362. "packetsReceived": "packetsReceived",
  363. "packetsSent": "packetsSent",
  364. "packetsLost": "packetsLost",
  365. "bytesReceived": "bytesReceived",
  366. "bytesSent": "bytesSent",
  367. "googFrameHeightReceived": "googFrameHeightReceived",
  368. "googFrameWidthReceived": "googFrameWidthReceived",
  369. "googFrameHeightSent": "googFrameHeightSent",
  370. "googFrameWidthSent": "googFrameWidthSent",
  371. "audioInputLevel": "audioInputLevel",
  372. "audioOutputLevel": "audioOutputLevel"
  373. };
  374. /**
  375. * Stats processing logic.
  376. */
  377. StatsCollector.prototype.processStatsReport = function () {
  378. if (!this.baselineStatsReport) {
  379. return;
  380. }
  381. for (var idx in this.currentStatsReport) {
  382. var now = this.currentStatsReport[idx];
  383. try {
  384. if (getStatValue(now, 'receiveBandwidth') ||
  385. getStatValue(now, 'sendBandwidth')) {
  386. PeerStats.bandwidth = {
  387. "download": Math.round(
  388. (getStatValue(now, 'receiveBandwidth')) / 1000),
  389. "upload": Math.round(
  390. (getStatValue(now, 'sendBandwidth')) / 1000)
  391. };
  392. }
  393. }
  394. catch(e){/*not supported*/}
  395. if(now.type == 'googCandidatePair')
  396. {
  397. var ip, type, localIP, active;
  398. try {
  399. ip = getStatValue(now, 'remoteAddress');
  400. type = getStatValue(now, "transportType");
  401. localIP = getStatValue(now, "localAddress");
  402. active = getStatValue(now, "activeConnection");
  403. }
  404. catch(e){/*not supported*/}
  405. if(!ip || !type || !localIP || active != "true")
  406. continue;
  407. var addressSaved = false;
  408. for(var i = 0; i < PeerStats.transport.length; i++)
  409. {
  410. if(PeerStats.transport[i].ip == ip &&
  411. PeerStats.transport[i].type == type &&
  412. PeerStats.transport[i].localip == localIP)
  413. {
  414. addressSaved = true;
  415. }
  416. }
  417. if(addressSaved)
  418. continue;
  419. PeerStats.transport.push({localip: localIP, ip: ip, type: type});
  420. continue;
  421. }
  422. if(now.type == "candidatepair")
  423. {
  424. if(now.state == "succeeded")
  425. continue;
  426. var local = this.currentStatsReport[now.localCandidateId];
  427. var remote = this.currentStatsReport[now.remoteCandidateId];
  428. PeerStats.transport.push({localip: local.ipAddress + ":" + local.portNumber,
  429. ip: remote.ipAddress + ":" + remote.portNumber, type: local.transport});
  430. }
  431. if (now.type != 'ssrc' && now.type != "outboundrtp" &&
  432. now.type != "inboundrtp") {
  433. continue;
  434. }
  435. var before = this.baselineStatsReport[idx];
  436. if (!before) {
  437. console.warn(getStatValue(now, 'ssrc') + ' not enough data');
  438. continue;
  439. }
  440. var ssrc = getStatValue(now, 'ssrc');
  441. if(!ssrc)
  442. continue;
  443. var jid = APP.xmpp.getJidFromSSRC(ssrc);
  444. if (!jid && (Date.now() - now.timestamp) < 3000) {
  445. console.warn("No jid for ssrc: " + ssrc);
  446. continue;
  447. }
  448. var jidStats = this.jid2stats[jid];
  449. if (!jidStats) {
  450. jidStats = new PeerStats();
  451. this.jid2stats[jid] = jidStats;
  452. }
  453. var isDownloadStream = true;
  454. var key = 'packetsReceived';
  455. if (!getStatValue(now, key))
  456. {
  457. isDownloadStream = false;
  458. key = 'packetsSent';
  459. if (!getStatValue(now, key))
  460. {
  461. console.warn("No packetsReceived nor packetSent stat found");
  462. continue;
  463. }
  464. }
  465. var packetsNow = getStatValue(now, key);
  466. if(!packetsNow || packetsNow < 0)
  467. packetsNow = 0;
  468. var packetsBefore = getStatValue(before, key);
  469. if(!packetsBefore || packetsBefore < 0)
  470. packetsBefore = 0;
  471. var packetRate = packetsNow - packetsBefore;
  472. if(!packetRate || packetRate < 0)
  473. packetRate = 0;
  474. var currentLoss = getStatValue(now, 'packetsLost');
  475. if(!currentLoss || currentLoss < 0)
  476. currentLoss = 0;
  477. var previousLoss = getStatValue(before, 'packetsLost');
  478. if(!previousLoss || previousLoss < 0)
  479. previousLoss = 0;
  480. var lossRate = currentLoss - previousLoss;
  481. if(!lossRate || lossRate < 0)
  482. lossRate = 0;
  483. var packetsTotal = (packetRate + lossRate);
  484. jidStats.setSsrcLoss(ssrc,
  485. {"packetsTotal": packetsTotal,
  486. "packetsLost": lossRate,
  487. "isDownloadStream": isDownloadStream});
  488. var bytesReceived = 0, bytesSent = 0;
  489. if(getStatValue(now, "bytesReceived"))
  490. {
  491. bytesReceived = getStatValue(now, "bytesReceived") -
  492. getStatValue(before, "bytesReceived");
  493. }
  494. if(getStatValue(now, "bytesSent"))
  495. {
  496. bytesSent = getStatValue(now, "bytesSent") -
  497. getStatValue(before, "bytesSent");
  498. }
  499. var time = Math.round((now.timestamp - before.timestamp) / 1000);
  500. if(bytesReceived <= 0 || time <= 0)
  501. {
  502. bytesReceived = 0;
  503. }
  504. else
  505. {
  506. bytesReceived = Math.round(((bytesReceived * 8) / time) / 1000);
  507. }
  508. if(bytesSent <= 0 || time <= 0)
  509. {
  510. bytesSent = 0;
  511. }
  512. else
  513. {
  514. bytesSent = Math.round(((bytesSent * 8) / time) / 1000);
  515. }
  516. jidStats.setSsrcBitrate(ssrc, {
  517. "download": bytesReceived,
  518. "upload": bytesSent});
  519. var resolution = {height: null, width: null};
  520. try {
  521. if (getStatValue(now, "googFrameHeightReceived") &&
  522. getStatValue(now, "googFrameWidthReceived")) {
  523. resolution.height = getStatValue(now, "googFrameHeightReceived");
  524. resolution.width = getStatValue(now, "googFrameWidthReceived");
  525. }
  526. else if (getStatValue(now, "googFrameHeightSent") &&
  527. getStatValue(now, "googFrameWidthSent")) {
  528. resolution.height = getStatValue(now, "googFrameHeightSent");
  529. resolution.width = getStatValue(now, "googFrameWidthSent");
  530. }
  531. }
  532. catch(e){/*not supported*/}
  533. if(resolution.height && resolution.width)
  534. {
  535. jidStats.setSsrcResolution(ssrc, resolution);
  536. }
  537. else
  538. {
  539. jidStats.setSsrcResolution(ssrc, null);
  540. }
  541. }
  542. var self = this;
  543. // Jid stats
  544. var totalPackets = {download: 0, upload: 0};
  545. var lostPackets = {download: 0, upload: 0};
  546. var bitrateDownload = 0;
  547. var bitrateUpload = 0;
  548. var resolutions = {};
  549. Object.keys(this.jid2stats).forEach(
  550. function (jid)
  551. {
  552. Object.keys(self.jid2stats[jid].ssrc2Loss).forEach(
  553. function (ssrc)
  554. {
  555. var type = "upload";
  556. if(self.jid2stats[jid].ssrc2Loss[ssrc].isDownloadStream)
  557. type = "download";
  558. totalPackets[type] +=
  559. self.jid2stats[jid].ssrc2Loss[ssrc].packetsTotal;
  560. lostPackets[type] +=
  561. self.jid2stats[jid].ssrc2Loss[ssrc].packetsLost;
  562. }
  563. );
  564. Object.keys(self.jid2stats[jid].ssrc2bitrate).forEach(
  565. function (ssrc) {
  566. bitrateDownload +=
  567. self.jid2stats[jid].ssrc2bitrate[ssrc].download;
  568. bitrateUpload +=
  569. self.jid2stats[jid].ssrc2bitrate[ssrc].upload;
  570. delete self.jid2stats[jid].ssrc2bitrate[ssrc];
  571. }
  572. );
  573. resolutions[jid] = self.jid2stats[jid].ssrc2resolution;
  574. }
  575. );
  576. PeerStats.bitrate = {"upload": bitrateUpload, "download": bitrateDownload};
  577. PeerStats.packetLoss = {
  578. total:
  579. calculatePacketLoss(lostPackets.download + lostPackets.upload,
  580. totalPackets.download + totalPackets.upload),
  581. download:
  582. calculatePacketLoss(lostPackets.download, totalPackets.download),
  583. upload:
  584. calculatePacketLoss(lostPackets.upload, totalPackets.upload)
  585. };
  586. this.eventEmitter.emit("statistics.connectionstats",
  587. {
  588. "bitrate": PeerStats.bitrate,
  589. "packetLoss": PeerStats.packetLoss,
  590. "bandwidth": PeerStats.bandwidth,
  591. "resolution": resolutions,
  592. "transport": PeerStats.transport
  593. });
  594. PeerStats.transport = [];
  595. };
  596. /**
  597. * Stats processing logic.
  598. */
  599. StatsCollector.prototype.processAudioLevelReport = function ()
  600. {
  601. if (!this.baselineAudioLevelsReport)
  602. {
  603. return;
  604. }
  605. for (var idx in this.currentAudioLevelsReport)
  606. {
  607. var now = this.currentAudioLevelsReport[idx];
  608. if (now.type != 'ssrc')
  609. {
  610. continue;
  611. }
  612. var before = this.baselineAudioLevelsReport[idx];
  613. if (!before)
  614. {
  615. console.warn(getStatValue(now, 'ssrc') + ' not enough data');
  616. continue;
  617. }
  618. var ssrc = getStatValue(now, 'ssrc');
  619. var jid = APP.xmpp.getJidFromSSRC(ssrc);
  620. if (!jid && (Date.now() - now.timestamp) < 3000)
  621. {
  622. console.warn("No jid for ssrc: " + ssrc);
  623. continue;
  624. }
  625. var jidStats = this.jid2stats[jid];
  626. if (!jidStats)
  627. {
  628. jidStats = new PeerStats();
  629. this.jid2stats[jid] = jidStats;
  630. }
  631. // Audio level
  632. var audioLevel = null;
  633. try {
  634. audioLevel = getStatValue(now, 'audioInputLevel');
  635. if (!audioLevel)
  636. audioLevel = getStatValue(now, 'audioOutputLevel');
  637. }
  638. catch(e) {/*not supported*/
  639. console.warn("Audio Levels are not available in the statistics.");
  640. clearInterval(this.audioLevelsIntervalId);
  641. return;
  642. }
  643. if (audioLevel)
  644. {
  645. // TODO: can't find specs about what this value really is,
  646. // but it seems to vary between 0 and around 32k.
  647. audioLevel = audioLevel / 32767;
  648. jidStats.setSsrcAudioLevel(ssrc, audioLevel);
  649. if(jid != APP.xmpp.myJid())
  650. this.eventEmitter.emit("statistics.audioLevel", jid, audioLevel);
  651. }
  652. }
  653. };