您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RTPStatsCollector.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /* global ssrc2jid */
  2. /* jshint -W117 */
  3. var RTCBrowserType = require("../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. var browserType = RTCBrowserType.getBrowserType();
  18. if (!keyMap[browserType][name])
  19. throw "The property isn't supported!";
  20. var key = keyMap[browserType][name];
  21. return (RTCBrowserType.isChrome() || RTCBrowserType.isOpera()) ?
  22. item.stat(key) : item[key];
  23. }
  24. /**
  25. * Peer statistics data holder.
  26. * @constructor
  27. */
  28. function PeerStats()
  29. {
  30. this.ssrc2Loss = {};
  31. this.ssrc2AudioLevel = {};
  32. this.ssrc2bitrate = {};
  33. this.ssrc2resolution = {};
  34. }
  35. /**
  36. * The bandwidth
  37. * @type {{}}
  38. */
  39. PeerStats.bandwidth = {};
  40. /**
  41. * The bit rate
  42. * @type {{}}
  43. */
  44. PeerStats.bitrate = {};
  45. /**
  46. * The packet loss rate
  47. * @type {{}}
  48. */
  49. PeerStats.packetLoss = null;
  50. /**
  51. * Sets packets loss rate for given <tt>ssrc</tt> that blong to the peer
  52. * represented by this instance.
  53. * @param ssrc audio or video RTP stream SSRC.
  54. * @param lossRate new packet loss rate value to be set.
  55. */
  56. PeerStats.prototype.setSsrcLoss = function (ssrc, lossRate)
  57. {
  58. this.ssrc2Loss[ssrc] = lossRate;
  59. };
  60. /**
  61. * Sets resolution for given <tt>ssrc</tt> that belong to the peer
  62. * represented by this instance.
  63. * @param ssrc audio or video RTP stream SSRC.
  64. * @param resolution new resolution value to be set.
  65. */
  66. PeerStats.prototype.setSsrcResolution = function (ssrc, resolution)
  67. {
  68. if(resolution === null && this.ssrc2resolution[ssrc])
  69. {
  70. delete this.ssrc2resolution[ssrc];
  71. }
  72. else if(resolution !== null)
  73. this.ssrc2resolution[ssrc] = resolution;
  74. };
  75. /**
  76. * Sets the bit rate for given <tt>ssrc</tt> that blong to the peer
  77. * represented by this instance.
  78. * @param ssrc audio or video RTP stream SSRC.
  79. * @param bitrate new bitrate value to be set.
  80. */
  81. PeerStats.prototype.setSsrcBitrate = function (ssrc, bitrate)
  82. {
  83. if(this.ssrc2bitrate[ssrc])
  84. {
  85. this.ssrc2bitrate[ssrc].download += bitrate.download;
  86. this.ssrc2bitrate[ssrc].upload += bitrate.upload;
  87. }
  88. else {
  89. this.ssrc2bitrate[ssrc] = bitrate;
  90. }
  91. };
  92. /**
  93. * Sets new audio level(input or output) for given <tt>ssrc</tt> that identifies
  94. * the stream which belongs to the peer represented by this instance.
  95. * @param ssrc RTP stream SSRC for which current audio level value will be
  96. * updated.
  97. * @param audioLevel the new audio level value to be set. Value is truncated to
  98. * fit the range from 0 to 1.
  99. */
  100. PeerStats.prototype.setSsrcAudioLevel = function (ssrc, audioLevel)
  101. {
  102. // Range limit 0 - 1
  103. this.ssrc2AudioLevel[ssrc] = formatAudioLevel(audioLevel);
  104. };
  105. function formatAudioLevel(audioLevel) {
  106. return Math.min(Math.max(audioLevel, 0), 1);
  107. }
  108. /**
  109. * Array with the transport information.
  110. * @type {Array}
  111. */
  112. PeerStats.transport = [];
  113. /**
  114. * <tt>StatsCollector</tt> registers for stats updates of given
  115. * <tt>peerconnection</tt> in given <tt>interval</tt>. On each update particular
  116. * stats are extracted and put in {@link PeerStats} objects. Once the processing
  117. * is done <tt>audioLevelsUpdateCallback</tt> is called with <tt>this</tt>
  118. * instance as an event source.
  119. *
  120. * @param peerconnection webRTC peer connection object.
  121. * @param interval stats refresh interval given in ms.
  122. * @param {function(StatsCollector)} audioLevelsUpdateCallback the callback
  123. * called on stats update.
  124. * @constructor
  125. */
  126. function StatsCollector(peerconnection, audioLevelsInterval, statsInterval, eventEmitter)
  127. {
  128. this.peerconnection = peerconnection;
  129. this.baselineAudioLevelsReport = null;
  130. this.currentAudioLevelsReport = null;
  131. this.currentStatsReport = null;
  132. this.baselineStatsReport = null;
  133. this.audioLevelsIntervalId = null;
  134. this.eventEmitter = eventEmitter;
  135. /**
  136. * Gather PeerConnection stats once every this many milliseconds.
  137. */
  138. this.GATHER_INTERVAL = 15000;
  139. /**
  140. * Log stats via the focus once every this many milliseconds.
  141. */
  142. this.LOG_INTERVAL = 60000;
  143. /**
  144. * Gather stats and store them in this.statsToBeLogged.
  145. */
  146. this.gatherStatsIntervalId = null;
  147. /**
  148. * Send the stats already saved in this.statsToBeLogged to be logged via
  149. * the focus.
  150. */
  151. this.logStatsIntervalId = null;
  152. /**
  153. * Stores the statistics which will be send to the focus to be logged.
  154. */
  155. this.statsToBeLogged =
  156. {
  157. timestamps: [],
  158. stats: {}
  159. };
  160. // Updates stats interval
  161. this.audioLevelsIntervalMilis = audioLevelsInterval;
  162. this.statsIntervalId = null;
  163. this.statsIntervalMilis = statsInterval;
  164. // Map of jids to PeerStats
  165. this.jid2stats = {};
  166. }
  167. module.exports = StatsCollector;
  168. /**
  169. * Stops stats updates.
  170. */
  171. StatsCollector.prototype.stop = function () {
  172. if (this.audioLevelsIntervalId) {
  173. clearInterval(this.audioLevelsIntervalId);
  174. this.audioLevelsIntervalId = null;
  175. }
  176. if (this.statsIntervalId)
  177. {
  178. clearInterval(this.statsIntervalId);
  179. this.statsIntervalId = null;
  180. }
  181. if(this.logStatsIntervalId)
  182. {
  183. clearInterval(this.logStatsIntervalId);
  184. this.logStatsIntervalId = null;
  185. }
  186. if(this.gatherStatsIntervalId)
  187. {
  188. clearInterval(this.gatherStatsIntervalId);
  189. this.gatherStatsIntervalId = null;
  190. }
  191. };
  192. /**
  193. * Callback passed to <tt>getStats</tt> method.
  194. * @param error an error that occurred on <tt>getStats</tt> call.
  195. */
  196. StatsCollector.prototype.errorCallback = function (error)
  197. {
  198. console.error("Get stats error", error);
  199. this.stop();
  200. };
  201. /**
  202. * Starts stats updates.
  203. */
  204. StatsCollector.prototype.start = function ()
  205. {
  206. var self = this;
  207. if (!config.disableAudioLevels) {
  208. this.audioLevelsIntervalId = setInterval(
  209. function () {
  210. // Interval updates
  211. self.peerconnection.getStats(
  212. function (report) {
  213. var results = null;
  214. if (!report || !report.result ||
  215. typeof report.result != 'function') {
  216. results = report;
  217. }
  218. else {
  219. results = report.result();
  220. }
  221. //console.error("Got interval report", results);
  222. self.currentAudioLevelsReport = results;
  223. self.processAudioLevelReport();
  224. self.baselineAudioLevelsReport =
  225. self.currentAudioLevelsReport;
  226. },
  227. self.errorCallback
  228. );
  229. },
  230. self.audioLevelsIntervalMilis
  231. );
  232. }
  233. if(!config.disableStats && !navigator.mozGetUserMedia) {
  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 && !navigator.mozGetUserMedia) {
  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. keyMap[RTCBrowserType.RTC_BROWSER_OPERA] =
  377. keyMap[RTCBrowserType.RTC_BROWSER_CHROME];
  378. /**
  379. * Stats processing logic.
  380. */
  381. StatsCollector.prototype.processStatsReport = function () {
  382. if (!this.baselineStatsReport) {
  383. return;
  384. }
  385. for (var idx in this.currentStatsReport) {
  386. var now = this.currentStatsReport[idx];
  387. try {
  388. if (getStatValue(now, 'receiveBandwidth') ||
  389. getStatValue(now, 'sendBandwidth')) {
  390. PeerStats.bandwidth = {
  391. "download": Math.round(
  392. (getStatValue(now, 'receiveBandwidth')) / 1000),
  393. "upload": Math.round(
  394. (getStatValue(now, 'sendBandwidth')) / 1000)
  395. };
  396. }
  397. }
  398. catch(e){/*not supported*/}
  399. if(now.type == 'googCandidatePair')
  400. {
  401. var ip, type, localIP, active;
  402. try {
  403. ip = getStatValue(now, 'remoteAddress');
  404. type = getStatValue(now, "transportType");
  405. localIP = getStatValue(now, "localAddress");
  406. active = getStatValue(now, "activeConnection");
  407. }
  408. catch(e){/*not supported*/}
  409. if(!ip || !type || !localIP || active != "true")
  410. continue;
  411. var addressSaved = false;
  412. for(var i = 0; i < PeerStats.transport.length; i++)
  413. {
  414. if(PeerStats.transport[i].ip == ip &&
  415. PeerStats.transport[i].type == type &&
  416. PeerStats.transport[i].localip == localIP)
  417. {
  418. addressSaved = true;
  419. }
  420. }
  421. if(addressSaved)
  422. continue;
  423. PeerStats.transport.push({localip: localIP, ip: ip, type: type});
  424. continue;
  425. }
  426. if(now.type == "candidatepair")
  427. {
  428. if(now.state == "succeeded")
  429. continue;
  430. var local = this.currentStatsReport[now.localCandidateId];
  431. var remote = this.currentStatsReport[now.remoteCandidateId];
  432. PeerStats.transport.push({localip: local.ipAddress + ":" + local.portNumber,
  433. ip: remote.ipAddress + ":" + remote.portNumber, type: local.transport});
  434. }
  435. if (now.type != 'ssrc' && now.type != "outboundrtp" &&
  436. now.type != "inboundrtp") {
  437. continue;
  438. }
  439. var before = this.baselineStatsReport[idx];
  440. if (!before) {
  441. console.warn(getStatValue(now, 'ssrc') + ' not enough data');
  442. continue;
  443. }
  444. var ssrc = getStatValue(now, 'ssrc');
  445. if(!ssrc)
  446. continue;
  447. var jid = APP.xmpp.getJidFromSSRC(ssrc);
  448. if (!jid && (Date.now() - now.timestamp) < 3000) {
  449. console.warn("No jid for ssrc: " + ssrc);
  450. continue;
  451. }
  452. var jidStats = this.jid2stats[jid];
  453. if (!jidStats) {
  454. jidStats = new PeerStats();
  455. this.jid2stats[jid] = jidStats;
  456. }
  457. var isDownloadStream = true;
  458. var key = 'packetsReceived';
  459. if (!getStatValue(now, key))
  460. {
  461. isDownloadStream = false;
  462. key = 'packetsSent';
  463. if (!getStatValue(now, key))
  464. {
  465. console.warn("No packetsReceived nor packetSent stat found");
  466. continue;
  467. }
  468. }
  469. var packetsNow = getStatValue(now, key);
  470. if(!packetsNow || packetsNow < 0)
  471. packetsNow = 0;
  472. var packetsBefore = getStatValue(before, key);
  473. if(!packetsBefore || packetsBefore < 0)
  474. packetsBefore = 0;
  475. var packetRate = packetsNow - packetsBefore;
  476. if(!packetRate || packetRate < 0)
  477. packetRate = 0;
  478. var currentLoss = getStatValue(now, 'packetsLost');
  479. if(!currentLoss || currentLoss < 0)
  480. currentLoss = 0;
  481. var previousLoss = getStatValue(before, 'packetsLost');
  482. if(!previousLoss || previousLoss < 0)
  483. previousLoss = 0;
  484. var lossRate = currentLoss - previousLoss;
  485. if(!lossRate || lossRate < 0)
  486. lossRate = 0;
  487. var packetsTotal = (packetRate + lossRate);
  488. jidStats.setSsrcLoss(ssrc,
  489. {"packetsTotal": packetsTotal,
  490. "packetsLost": lossRate,
  491. "isDownloadStream": isDownloadStream});
  492. var bytesReceived = 0, bytesSent = 0;
  493. if(getStatValue(now, "bytesReceived"))
  494. {
  495. bytesReceived = getStatValue(now, "bytesReceived") -
  496. getStatValue(before, "bytesReceived");
  497. }
  498. if(getStatValue(now, "bytesSent"))
  499. {
  500. bytesSent = getStatValue(now, "bytesSent") -
  501. getStatValue(before, "bytesSent");
  502. }
  503. var time = Math.round((now.timestamp - before.timestamp) / 1000);
  504. if(bytesReceived <= 0 || time <= 0)
  505. {
  506. bytesReceived = 0;
  507. }
  508. else
  509. {
  510. bytesReceived = Math.round(((bytesReceived * 8) / time) / 1000);
  511. }
  512. if(bytesSent <= 0 || time <= 0)
  513. {
  514. bytesSent = 0;
  515. }
  516. else
  517. {
  518. bytesSent = Math.round(((bytesSent * 8) / time) / 1000);
  519. }
  520. jidStats.setSsrcBitrate(ssrc, {
  521. "download": bytesReceived,
  522. "upload": bytesSent});
  523. var resolution = {height: null, width: null};
  524. try {
  525. if (getStatValue(now, "googFrameHeightReceived") &&
  526. getStatValue(now, "googFrameWidthReceived")) {
  527. resolution.height = getStatValue(now, "googFrameHeightReceived");
  528. resolution.width = getStatValue(now, "googFrameWidthReceived");
  529. }
  530. else if (getStatValue(now, "googFrameHeightSent") &&
  531. getStatValue(now, "googFrameWidthSent")) {
  532. resolution.height = getStatValue(now, "googFrameHeightSent");
  533. resolution.width = getStatValue(now, "googFrameWidthSent");
  534. }
  535. }
  536. catch(e){/*not supported*/}
  537. if(resolution.height && resolution.width)
  538. {
  539. jidStats.setSsrcResolution(ssrc, resolution);
  540. }
  541. else
  542. {
  543. jidStats.setSsrcResolution(ssrc, null);
  544. }
  545. }
  546. var self = this;
  547. // Jid stats
  548. var totalPackets = {download: 0, upload: 0};
  549. var lostPackets = {download: 0, upload: 0};
  550. var bitrateDownload = 0;
  551. var bitrateUpload = 0;
  552. var resolutions = {};
  553. Object.keys(this.jid2stats).forEach(
  554. function (jid)
  555. {
  556. Object.keys(self.jid2stats[jid].ssrc2Loss).forEach(
  557. function (ssrc)
  558. {
  559. var type = "upload";
  560. if(self.jid2stats[jid].ssrc2Loss[ssrc].isDownloadStream)
  561. type = "download";
  562. totalPackets[type] +=
  563. self.jid2stats[jid].ssrc2Loss[ssrc].packetsTotal;
  564. lostPackets[type] +=
  565. self.jid2stats[jid].ssrc2Loss[ssrc].packetsLost;
  566. }
  567. );
  568. Object.keys(self.jid2stats[jid].ssrc2bitrate).forEach(
  569. function (ssrc) {
  570. bitrateDownload +=
  571. self.jid2stats[jid].ssrc2bitrate[ssrc].download;
  572. bitrateUpload +=
  573. self.jid2stats[jid].ssrc2bitrate[ssrc].upload;
  574. delete self.jid2stats[jid].ssrc2bitrate[ssrc];
  575. }
  576. );
  577. resolutions[jid] = self.jid2stats[jid].ssrc2resolution;
  578. }
  579. );
  580. PeerStats.bitrate = {"upload": bitrateUpload, "download": bitrateDownload};
  581. PeerStats.packetLoss = {
  582. total:
  583. calculatePacketLoss(lostPackets.download + lostPackets.upload,
  584. totalPackets.download + totalPackets.upload),
  585. download:
  586. calculatePacketLoss(lostPackets.download, totalPackets.download),
  587. upload:
  588. calculatePacketLoss(lostPackets.upload, totalPackets.upload)
  589. };
  590. this.eventEmitter.emit("statistics.connectionstats",
  591. {
  592. "bitrate": PeerStats.bitrate,
  593. "packetLoss": PeerStats.packetLoss,
  594. "bandwidth": PeerStats.bandwidth,
  595. "resolution": resolutions,
  596. "transport": PeerStats.transport
  597. });
  598. PeerStats.transport = [];
  599. };
  600. /**
  601. * Stats processing logic.
  602. */
  603. StatsCollector.prototype.processAudioLevelReport = function () {
  604. if (!this.baselineAudioLevelsReport) {
  605. return;
  606. }
  607. for (var idx in this.currentAudioLevelsReport) {
  608. var now = this.currentAudioLevelsReport[idx];
  609. if (now.type != 'ssrc') {
  610. continue;
  611. }
  612. var before = this.baselineAudioLevelsReport[idx];
  613. if (!before) {
  614. console.warn(getStatValue(now, 'ssrc') + ' not enough data');
  615. continue;
  616. }
  617. var ssrc = getStatValue(now, 'ssrc');
  618. var jid = APP.xmpp.getJidFromSSRC(ssrc);
  619. if (!jid) {
  620. if((Date.now() - now.timestamp) < 3000)
  621. console.warn("No jid for ssrc: " + ssrc);
  622. continue;
  623. }
  624. var jidStats = this.jid2stats[jid];
  625. if (!jidStats) {
  626. jidStats = new PeerStats();
  627. this.jid2stats[jid] = jidStats;
  628. }
  629. // Audio level
  630. var audioLevel = null;
  631. try {
  632. audioLevel = getStatValue(now, 'audioInputLevel');
  633. if (!audioLevel)
  634. audioLevel = getStatValue(now, 'audioOutputLevel');
  635. }
  636. catch(e) {/*not supported*/
  637. console.warn("Audio Levels are not available in the statistics.");
  638. clearInterval(this.audioLevelsIntervalId);
  639. return;
  640. }
  641. if (audioLevel) {
  642. // TODO: can't find specs about what this value really is,
  643. // but it seems to vary between 0 and around 32k.
  644. audioLevel = audioLevel / 32767;
  645. jidStats.setSsrcAudioLevel(ssrc, audioLevel);
  646. if(jid != APP.xmpp.myJid())
  647. this.eventEmitter.emit("statistics.audioLevel", jid, audioLevel);
  648. }
  649. }
  650. };