選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RTPStatsCollector.js 22KB

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