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.

statistics.bundle.js 36KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.statistics=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. /**
  3. * Provides statistics for the local stream.
  4. */
  5. /**
  6. * Size of the webaudio analizer buffer.
  7. * @type {number}
  8. */
  9. var WEBAUDIO_ANALIZER_FFT_SIZE = 2048;
  10. /**
  11. * Value of the webaudio analizer smoothing time parameter.
  12. * @type {number}
  13. */
  14. var WEBAUDIO_ANALIZER_SMOOTING_TIME = 0.8;
  15. /**
  16. * Converts time domain data array to audio level.
  17. * @param array the time domain data array.
  18. * @returns {number} the audio level
  19. */
  20. function timeDomainDataToAudioLevel(samples) {
  21. var maxVolume = 0;
  22. var length = samples.length;
  23. for (var i = 0; i < length; i++) {
  24. if (maxVolume < samples[i])
  25. maxVolume = samples[i];
  26. }
  27. return parseFloat(((maxVolume - 127) / 128).toFixed(3));
  28. };
  29. /**
  30. * Animates audio level change
  31. * @param newLevel the new audio level
  32. * @param lastLevel the last audio level
  33. * @returns {Number} the audio level to be set
  34. */
  35. function animateLevel(newLevel, lastLevel)
  36. {
  37. var value = 0;
  38. var diff = lastLevel - newLevel;
  39. if(diff > 0.2)
  40. {
  41. value = lastLevel - 0.2;
  42. }
  43. else if(diff < -0.4)
  44. {
  45. value = lastLevel + 0.4;
  46. }
  47. else
  48. {
  49. value = newLevel;
  50. }
  51. return parseFloat(value.toFixed(3));
  52. }
  53. /**
  54. * <tt>LocalStatsCollector</tt> calculates statistics for the local stream.
  55. *
  56. * @param stream the local stream
  57. * @param interval stats refresh interval given in ms.
  58. * @param {function(LocalStatsCollector)} updateCallback the callback called on stats
  59. * update.
  60. * @constructor
  61. */
  62. function LocalStatsCollector(stream, interval, statisticsService, eventEmitter) {
  63. window.AudioContext = window.AudioContext || window.webkitAudioContext;
  64. this.stream = stream;
  65. this.intervalId = null;
  66. this.intervalMilis = interval;
  67. this.eventEmitter = eventEmitter;
  68. this.audioLevel = 0;
  69. this.statisticsService = statisticsService;
  70. }
  71. /**
  72. * Starts the collecting the statistics.
  73. */
  74. LocalStatsCollector.prototype.start = function () {
  75. if (!window.AudioContext)
  76. return;
  77. var context = new AudioContext();
  78. var analyser = context.createAnalyser();
  79. analyser.smoothingTimeConstant = WEBAUDIO_ANALIZER_SMOOTING_TIME;
  80. analyser.fftSize = WEBAUDIO_ANALIZER_FFT_SIZE;
  81. var source = context.createMediaStreamSource(this.stream);
  82. source.connect(analyser);
  83. var self = this;
  84. this.intervalId = setInterval(
  85. function () {
  86. var array = new Uint8Array(analyser.frequencyBinCount);
  87. analyser.getByteTimeDomainData(array);
  88. var audioLevel = timeDomainDataToAudioLevel(array);
  89. if(audioLevel != self.audioLevel) {
  90. self.audioLevel = animateLevel(audioLevel, self.audioLevel);
  91. self.eventEmitter.emit(
  92. "statistics.audioLevel",
  93. self.statisticsService.LOCAL_JID,
  94. self.audioLevel);
  95. }
  96. },
  97. this.intervalMilis
  98. );
  99. };
  100. /**
  101. * Stops collecting the statistics.
  102. */
  103. LocalStatsCollector.prototype.stop = function () {
  104. if (this.intervalId) {
  105. clearInterval(this.intervalId);
  106. this.intervalId = null;
  107. }
  108. };
  109. module.exports = LocalStatsCollector;
  110. },{}],2:[function(require,module,exports){
  111. /* global focusMucJid, ssrc2jid */
  112. /* jshint -W117 */
  113. /**
  114. * Calculates packet lost percent using the number of lost packets and the
  115. * number of all packet.
  116. * @param lostPackets the number of lost packets
  117. * @param totalPackets the number of all packets.
  118. * @returns {number} packet loss percent
  119. */
  120. function calculatePacketLoss(lostPackets, totalPackets) {
  121. if(!totalPackets || totalPackets <= 0 || !lostPackets || lostPackets <= 0)
  122. return 0;
  123. return Math.round((lostPackets/totalPackets)*100);
  124. }
  125. function getStatValue(item, name) {
  126. if(!keyMap[RTC.browser][name])
  127. throw "The property isn't supported!";
  128. var key = keyMap[RTC.browser][name];
  129. return RTC.browser == "chrome"? item.stat(key) : item[key];
  130. }
  131. /**
  132. * Peer statistics data holder.
  133. * @constructor
  134. */
  135. function PeerStats()
  136. {
  137. this.ssrc2Loss = {};
  138. this.ssrc2AudioLevel = {};
  139. this.ssrc2bitrate = {};
  140. this.ssrc2resolution = {};
  141. }
  142. /**
  143. * The bandwidth
  144. * @type {{}}
  145. */
  146. PeerStats.bandwidth = {};
  147. /**
  148. * The bit rate
  149. * @type {{}}
  150. */
  151. PeerStats.bitrate = {};
  152. /**
  153. * The packet loss rate
  154. * @type {{}}
  155. */
  156. PeerStats.packetLoss = null;
  157. /**
  158. * Sets packets loss rate for given <tt>ssrc</tt> that blong to the peer
  159. * represented by this instance.
  160. * @param ssrc audio or video RTP stream SSRC.
  161. * @param lossRate new packet loss rate value to be set.
  162. */
  163. PeerStats.prototype.setSsrcLoss = function (ssrc, lossRate)
  164. {
  165. this.ssrc2Loss[ssrc] = lossRate;
  166. };
  167. /**
  168. * Sets resolution for given <tt>ssrc</tt> that belong to the peer
  169. * represented by this instance.
  170. * @param ssrc audio or video RTP stream SSRC.
  171. * @param resolution new resolution value to be set.
  172. */
  173. PeerStats.prototype.setSsrcResolution = function (ssrc, resolution)
  174. {
  175. if(resolution === null && this.ssrc2resolution[ssrc])
  176. {
  177. delete this.ssrc2resolution[ssrc];
  178. }
  179. else if(resolution !== null)
  180. this.ssrc2resolution[ssrc] = resolution;
  181. };
  182. /**
  183. * Sets the bit rate for given <tt>ssrc</tt> that blong to the peer
  184. * represented by this instance.
  185. * @param ssrc audio or video RTP stream SSRC.
  186. * @param bitrate new bitrate value to be set.
  187. */
  188. PeerStats.prototype.setSsrcBitrate = function (ssrc, bitrate)
  189. {
  190. if(this.ssrc2bitrate[ssrc])
  191. {
  192. this.ssrc2bitrate[ssrc].download += bitrate.download;
  193. this.ssrc2bitrate[ssrc].upload += bitrate.upload;
  194. }
  195. else {
  196. this.ssrc2bitrate[ssrc] = bitrate;
  197. }
  198. };
  199. /**
  200. * Sets new audio level(input or output) for given <tt>ssrc</tt> that identifies
  201. * the stream which belongs to the peer represented by this instance.
  202. * @param ssrc RTP stream SSRC for which current audio level value will be
  203. * updated.
  204. * @param audioLevel the new audio level value to be set. Value is truncated to
  205. * fit the range from 0 to 1.
  206. */
  207. PeerStats.prototype.setSsrcAudioLevel = function (ssrc, audioLevel)
  208. {
  209. // Range limit 0 - 1
  210. this.ssrc2AudioLevel[ssrc] = Math.min(Math.max(audioLevel, 0), 1);
  211. };
  212. /**
  213. * Array with the transport information.
  214. * @type {Array}
  215. */
  216. PeerStats.transport = [];
  217. /**
  218. * <tt>StatsCollector</tt> registers for stats updates of given
  219. * <tt>peerconnection</tt> in given <tt>interval</tt>. On each update particular
  220. * stats are extracted and put in {@link PeerStats} objects. Once the processing
  221. * is done <tt>audioLevelsUpdateCallback</tt> is called with <tt>this</tt>
  222. * instance as an event source.
  223. *
  224. * @param peerconnection webRTC peer connection object.
  225. * @param interval stats refresh interval given in ms.
  226. * @param {function(StatsCollector)} audioLevelsUpdateCallback the callback
  227. * called on stats update.
  228. * @constructor
  229. */
  230. function StatsCollector(peerconnection, audioLevelsInterval, statsInterval, eventEmitter)
  231. {
  232. this.peerconnection = peerconnection;
  233. this.baselineAudioLevelsReport = null;
  234. this.currentAudioLevelsReport = null;
  235. this.currentStatsReport = null;
  236. this.baselineStatsReport = null;
  237. this.audioLevelsIntervalId = null;
  238. this.eventEmitter = eventEmitter;
  239. /**
  240. * Gather PeerConnection stats once every this many milliseconds.
  241. */
  242. this.GATHER_INTERVAL = 10000;
  243. /**
  244. * Log stats via the focus once every this many milliseconds.
  245. */
  246. this.LOG_INTERVAL = 60000;
  247. /**
  248. * Gather stats and store them in this.statsToBeLogged.
  249. */
  250. this.gatherStatsIntervalId = null;
  251. /**
  252. * Send the stats already saved in this.statsToBeLogged to be logged via
  253. * the focus.
  254. */
  255. this.logStatsIntervalId = null;
  256. /**
  257. * Stores the statistics which will be send to the focus to be logged.
  258. */
  259. this.statsToBeLogged =
  260. {
  261. timestamps: [],
  262. stats: {}
  263. };
  264. // Updates stats interval
  265. this.audioLevelsIntervalMilis = audioLevelsInterval;
  266. this.statsIntervalId = null;
  267. this.statsIntervalMilis = statsInterval;
  268. // Map of jids to PeerStats
  269. this.jid2stats = {};
  270. }
  271. module.exports = StatsCollector;
  272. /**
  273. * Stops stats updates.
  274. */
  275. StatsCollector.prototype.stop = function ()
  276. {
  277. if (this.audioLevelsIntervalId)
  278. {
  279. clearInterval(this.audioLevelsIntervalId);
  280. this.audioLevelsIntervalId = null;
  281. clearInterval(this.statsIntervalId);
  282. this.statsIntervalId = null;
  283. clearInterval(this.logStatsIntervalId);
  284. this.logStatsIntervalId = null;
  285. clearInterval(this.gatherStatsIntervalId);
  286. this.gatherStatsIntervalId = null;
  287. }
  288. };
  289. /**
  290. * Callback passed to <tt>getStats</tt> method.
  291. * @param error an error that occurred on <tt>getStats</tt> call.
  292. */
  293. StatsCollector.prototype.errorCallback = function (error)
  294. {
  295. console.error("Get stats error", error);
  296. this.stop();
  297. };
  298. /**
  299. * Starts stats updates.
  300. */
  301. StatsCollector.prototype.start = function ()
  302. {
  303. var self = this;
  304. this.audioLevelsIntervalId = setInterval(
  305. function ()
  306. {
  307. // Interval updates
  308. self.peerconnection.getStats(
  309. function (report)
  310. {
  311. var results = null;
  312. if(!report || !report.result || typeof report.result != 'function')
  313. {
  314. results = report;
  315. }
  316. else
  317. {
  318. results = report.result();
  319. }
  320. //console.error("Got interval report", results);
  321. self.currentAudioLevelsReport = results;
  322. self.processAudioLevelReport();
  323. self.baselineAudioLevelsReport =
  324. self.currentAudioLevelsReport;
  325. },
  326. self.errorCallback
  327. );
  328. },
  329. self.audioLevelsIntervalMilis
  330. );
  331. this.statsIntervalId = setInterval(
  332. function () {
  333. // Interval updates
  334. self.peerconnection.getStats(
  335. function (report)
  336. {
  337. var results = null;
  338. if(!report || !report.result || typeof report.result != 'function')
  339. {
  340. //firefox
  341. results = report;
  342. }
  343. else
  344. {
  345. //chrome
  346. results = report.result();
  347. }
  348. //console.error("Got interval report", results);
  349. self.currentStatsReport = results;
  350. try
  351. {
  352. self.processStatsReport();
  353. }
  354. catch(e)
  355. {
  356. console.error("Unsupported key:" + e);
  357. }
  358. self.baselineStatsReport = self.currentStatsReport;
  359. },
  360. self.errorCallback
  361. );
  362. },
  363. self.statsIntervalMilis
  364. );
  365. if (config.logStats) {
  366. this.gatherStatsIntervalId = setInterval(
  367. function () {
  368. self.peerconnection.getStats(
  369. function (report) {
  370. self.addStatsToBeLogged(report.result());
  371. },
  372. function () {
  373. }
  374. );
  375. },
  376. this.GATHER_INTERVAL
  377. );
  378. this.logStatsIntervalId = setInterval(
  379. function() { self.logStats(); },
  380. this.LOG_INTERVAL);
  381. }
  382. };
  383. /**
  384. * Converts the stats to the format used for logging, and saves the data in
  385. * this.statsToBeLogged.
  386. * @param reports Reports as given by webkitRTCPerConnection.getStats.
  387. */
  388. StatsCollector.prototype.addStatsToBeLogged = function (reports) {
  389. var self = this;
  390. var num_records = this.statsToBeLogged.timestamps.length;
  391. this.statsToBeLogged.timestamps.push(new Date().getTime());
  392. reports.map(function (report) {
  393. var stat = self.statsToBeLogged.stats[report.id];
  394. if (!stat) {
  395. stat = self.statsToBeLogged.stats[report.id] = {};
  396. }
  397. stat.type = report.type;
  398. report.names().map(function (name) {
  399. var values = stat[name];
  400. if (!values) {
  401. values = stat[name] = [];
  402. }
  403. while (values.length < num_records) {
  404. values.push(null);
  405. }
  406. values.push(report.stat(name));
  407. });
  408. });
  409. };
  410. StatsCollector.prototype.logStats = function () {
  411. if (!focusMucJid) {
  412. return;
  413. }
  414. var deflate = true;
  415. var content = JSON.stringify(this.statsToBeLogged);
  416. if (deflate) {
  417. content = String.fromCharCode.apply(null, Pako.deflateRaw(content));
  418. }
  419. content = Base64.encode(content);
  420. // XEP-0337-ish
  421. var message = $msg({to: focusMucJid, type: 'normal'});
  422. message.c('log', { xmlns: 'urn:xmpp:eventlog',
  423. id: 'PeerConnectionStats'});
  424. message.c('message').t(content).up();
  425. if (deflate) {
  426. message.c('tag', {name: "deflated", value: "true"}).up();
  427. }
  428. message.up();
  429. connection.send(message);
  430. // Reset the stats
  431. this.statsToBeLogged.stats = {};
  432. this.statsToBeLogged.timestamps = [];
  433. };
  434. var keyMap = {
  435. "firefox": {
  436. "ssrc": "ssrc",
  437. "packetsReceived": "packetsReceived",
  438. "packetsLost": "packetsLost",
  439. "packetsSent": "packetsSent",
  440. "bytesReceived": "bytesReceived",
  441. "bytesSent": "bytesSent"
  442. },
  443. "chrome": {
  444. "receiveBandwidth": "googAvailableReceiveBandwidth",
  445. "sendBandwidth": "googAvailableSendBandwidth",
  446. "remoteAddress": "googRemoteAddress",
  447. "transportType": "googTransportType",
  448. "localAddress": "googLocalAddress",
  449. "activeConnection": "googActiveConnection",
  450. "ssrc": "ssrc",
  451. "packetsReceived": "packetsReceived",
  452. "packetsSent": "packetsSent",
  453. "packetsLost": "packetsLost",
  454. "bytesReceived": "bytesReceived",
  455. "bytesSent": "bytesSent",
  456. "googFrameHeightReceived": "googFrameHeightReceived",
  457. "googFrameWidthReceived": "googFrameWidthReceived",
  458. "googFrameHeightSent": "googFrameHeightSent",
  459. "googFrameWidthSent": "googFrameWidthSent",
  460. "audioInputLevel": "audioInputLevel",
  461. "audioOutputLevel": "audioOutputLevel"
  462. }
  463. };
  464. /**
  465. * Stats processing logic.
  466. */
  467. StatsCollector.prototype.processStatsReport = function () {
  468. if (!this.baselineStatsReport) {
  469. return;
  470. }
  471. for (var idx in this.currentStatsReport) {
  472. var now = this.currentStatsReport[idx];
  473. try {
  474. if (getStatValue(now, 'receiveBandwidth') ||
  475. getStatValue(now, 'sendBandwidth')) {
  476. PeerStats.bandwidth = {
  477. "download": Math.round(
  478. (getStatValue(now, 'receiveBandwidth')) / 1000),
  479. "upload": Math.round(
  480. (getStatValue(now, 'sendBandwidth')) / 1000)
  481. };
  482. }
  483. }
  484. catch(e){/*not supported*/}
  485. if(now.type == 'googCandidatePair')
  486. {
  487. var ip, type, localIP, active;
  488. try {
  489. ip = getStatValue(now, 'remoteAddress');
  490. type = getStatValue(now, "transportType");
  491. localIP = getStatValue(now, "localAddress");
  492. active = getStatValue(now, "activeConnection");
  493. }
  494. catch(e){/*not supported*/}
  495. if(!ip || !type || !localIP || active != "true")
  496. continue;
  497. var addressSaved = false;
  498. for(var i = 0; i < PeerStats.transport.length; i++)
  499. {
  500. if(PeerStats.transport[i].ip == ip &&
  501. PeerStats.transport[i].type == type &&
  502. PeerStats.transport[i].localip == localIP)
  503. {
  504. addressSaved = true;
  505. }
  506. }
  507. if(addressSaved)
  508. continue;
  509. PeerStats.transport.push({localip: localIP, ip: ip, type: type});
  510. continue;
  511. }
  512. if(now.type == "candidatepair")
  513. {
  514. if(now.state == "succeeded")
  515. continue;
  516. var local = this.currentStatsReport[now.localCandidateId];
  517. var remote = this.currentStatsReport[now.remoteCandidateId];
  518. PeerStats.transport.push({localip: local.ipAddress + ":" + local.portNumber,
  519. ip: remote.ipAddress + ":" + remote.portNumber, type: local.transport});
  520. }
  521. if (now.type != 'ssrc' && now.type != "outboundrtp" &&
  522. now.type != "inboundrtp") {
  523. continue;
  524. }
  525. var before = this.baselineStatsReport[idx];
  526. if (!before) {
  527. console.warn(getStatValue(now, 'ssrc') + ' not enough data');
  528. continue;
  529. }
  530. var ssrc = getStatValue(now, 'ssrc');
  531. if(!ssrc)
  532. continue;
  533. var jid = ssrc2jid[ssrc];
  534. if (!jid) {
  535. console.warn("No jid for ssrc: " + ssrc);
  536. continue;
  537. }
  538. var jidStats = this.jid2stats[jid];
  539. if (!jidStats) {
  540. jidStats = new PeerStats();
  541. this.jid2stats[jid] = jidStats;
  542. }
  543. var isDownloadStream = true;
  544. var key = 'packetsReceived';
  545. if (!getStatValue(now, key))
  546. {
  547. isDownloadStream = false;
  548. key = 'packetsSent';
  549. if (!getStatValue(now, key))
  550. {
  551. console.warn("No packetsReceived nor packetSent stat found");
  552. continue;
  553. }
  554. }
  555. var packetsNow = getStatValue(now, key);
  556. if(!packetsNow || packetsNow < 0)
  557. packetsNow = 0;
  558. var packetsBefore = getStatValue(before, key);
  559. if(!packetsBefore || packetsBefore < 0)
  560. packetsBefore = 0;
  561. var packetRate = packetsNow - packetsBefore;
  562. if(!packetRate || packetRate < 0)
  563. packetRate = 0;
  564. var currentLoss = getStatValue(now, 'packetsLost');
  565. if(!currentLoss || currentLoss < 0)
  566. currentLoss = 0;
  567. var previousLoss = getStatValue(before, 'packetsLost');
  568. if(!previousLoss || previousLoss < 0)
  569. previousLoss = 0;
  570. var lossRate = currentLoss - previousLoss;
  571. if(!lossRate || lossRate < 0)
  572. lossRate = 0;
  573. var packetsTotal = (packetRate + lossRate);
  574. jidStats.setSsrcLoss(ssrc,
  575. {"packetsTotal": packetsTotal,
  576. "packetsLost": lossRate,
  577. "isDownloadStream": isDownloadStream});
  578. var bytesReceived = 0, bytesSent = 0;
  579. if(getStatValue(now, "bytesReceived"))
  580. {
  581. bytesReceived = getStatValue(now, "bytesReceived") -
  582. getStatValue(before, "bytesReceived");
  583. }
  584. if(getStatValue(now, "bytesSent"))
  585. {
  586. bytesSent = getStatValue(now, "bytesSent") -
  587. getStatValue(before, "bytesSent");
  588. }
  589. var time = Math.round((now.timestamp - before.timestamp) / 1000);
  590. if(bytesReceived <= 0 || time <= 0)
  591. {
  592. bytesReceived = 0;
  593. }
  594. else
  595. {
  596. bytesReceived = Math.round(((bytesReceived * 8) / time) / 1000);
  597. }
  598. if(bytesSent <= 0 || time <= 0)
  599. {
  600. bytesSent = 0;
  601. }
  602. else
  603. {
  604. bytesSent = Math.round(((bytesSent * 8) / time) / 1000);
  605. }
  606. jidStats.setSsrcBitrate(ssrc, {
  607. "download": bytesReceived,
  608. "upload": bytesSent});
  609. var resolution = {height: null, width: null};
  610. try {
  611. if (getStatValue(now, "googFrameHeightReceived") &&
  612. getStatValue(now, "googFrameWidthReceived")) {
  613. resolution.height = getStatValue(now, "googFrameHeightReceived");
  614. resolution.width = getStatValue(now, "googFrameWidthReceived");
  615. }
  616. else if (getStatValue(now, "googFrameHeightSent") &&
  617. getStatValue(now, "googFrameWidthSent")) {
  618. resolution.height = getStatValue(now, "googFrameHeightSent");
  619. resolution.width = getStatValue(now, "googFrameWidthSent");
  620. }
  621. }
  622. catch(e){/*not supported*/}
  623. if(resolution.height && resolution.width)
  624. {
  625. jidStats.setSsrcResolution(ssrc, resolution);
  626. }
  627. else
  628. {
  629. jidStats.setSsrcResolution(ssrc, null);
  630. }
  631. }
  632. var self = this;
  633. // Jid stats
  634. var totalPackets = {download: 0, upload: 0};
  635. var lostPackets = {download: 0, upload: 0};
  636. var bitrateDownload = 0;
  637. var bitrateUpload = 0;
  638. var resolutions = {};
  639. Object.keys(this.jid2stats).forEach(
  640. function (jid)
  641. {
  642. Object.keys(self.jid2stats[jid].ssrc2Loss).forEach(
  643. function (ssrc)
  644. {
  645. var type = "upload";
  646. if(self.jid2stats[jid].ssrc2Loss[ssrc].isDownloadStream)
  647. type = "download";
  648. totalPackets[type] +=
  649. self.jid2stats[jid].ssrc2Loss[ssrc].packetsTotal;
  650. lostPackets[type] +=
  651. self.jid2stats[jid].ssrc2Loss[ssrc].packetsLost;
  652. }
  653. );
  654. Object.keys(self.jid2stats[jid].ssrc2bitrate).forEach(
  655. function (ssrc) {
  656. bitrateDownload +=
  657. self.jid2stats[jid].ssrc2bitrate[ssrc].download;
  658. bitrateUpload +=
  659. self.jid2stats[jid].ssrc2bitrate[ssrc].upload;
  660. delete self.jid2stats[jid].ssrc2bitrate[ssrc];
  661. }
  662. );
  663. resolutions[jid] = self.jid2stats[jid].ssrc2resolution;
  664. }
  665. );
  666. PeerStats.bitrate = {"upload": bitrateUpload, "download": bitrateDownload};
  667. PeerStats.packetLoss = {
  668. total:
  669. calculatePacketLoss(lostPackets.download + lostPackets.upload,
  670. totalPackets.download + totalPackets.upload),
  671. download:
  672. calculatePacketLoss(lostPackets.download, totalPackets.download),
  673. upload:
  674. calculatePacketLoss(lostPackets.upload, totalPackets.upload)
  675. };
  676. this.eventEmitter.emit("statistics.connectionstats",
  677. {
  678. "bitrate": PeerStats.bitrate,
  679. "packetLoss": PeerStats.packetLoss,
  680. "bandwidth": PeerStats.bandwidth,
  681. "resolution": resolutions,
  682. "transport": PeerStats.transport
  683. });
  684. PeerStats.transport = [];
  685. };
  686. /**
  687. * Stats processing logic.
  688. */
  689. StatsCollector.prototype.processAudioLevelReport = function ()
  690. {
  691. if (!this.baselineAudioLevelsReport)
  692. {
  693. return;
  694. }
  695. for (var idx in this.currentAudioLevelsReport)
  696. {
  697. var now = this.currentAudioLevelsReport[idx];
  698. if (now.type != 'ssrc')
  699. {
  700. continue;
  701. }
  702. var before = this.baselineAudioLevelsReport[idx];
  703. if (!before)
  704. {
  705. console.warn(getStatValue(now, 'ssrc') + ' not enough data');
  706. continue;
  707. }
  708. var ssrc = getStatValue(now, 'ssrc');
  709. var jid = ssrc2jid[ssrc];
  710. if (!jid)
  711. {
  712. console.warn("No jid for ssrc: " + ssrc);
  713. continue;
  714. }
  715. var jidStats = this.jid2stats[jid];
  716. if (!jidStats)
  717. {
  718. jidStats = new PeerStats();
  719. this.jid2stats[jid] = jidStats;
  720. }
  721. // Audio level
  722. var audioLevel = null;
  723. try {
  724. audioLevel = getStatValue(now, 'audioInputLevel');
  725. if (!audioLevel)
  726. audioLevel = getStatValue(now, 'audioOutputLevel');
  727. }
  728. catch(e) {/*not supported*/
  729. console.warn("Audio Levels are not available in the statistics.");
  730. clearInterval(this.audioLevelsIntervalId);
  731. return;
  732. }
  733. if (audioLevel)
  734. {
  735. // TODO: can't find specs about what this value really is,
  736. // but it seems to vary between 0 and around 32k.
  737. audioLevel = audioLevel / 32767;
  738. jidStats.setSsrcAudioLevel(ssrc, audioLevel);
  739. if(jid != connection.emuc.myroomjid)
  740. this.eventEmitter.emit("statistics.audioLevel", jid, audioLevel);
  741. }
  742. }
  743. };
  744. },{}],3:[function(require,module,exports){
  745. /**
  746. * Created by hristo on 8/4/14.
  747. */
  748. var LocalStats = require("./LocalStatsCollector.js");
  749. var RTPStats = require("./RTPStatsCollector.js");
  750. var EventEmitter = require("events");
  751. //var StreamEventTypes = require("../service/RTC/StreamEventTypes.js");
  752. //var XMPPEvents = require("../service/xmpp/XMPPEvents");
  753. var eventEmitter = new EventEmitter();
  754. var localStats = null;
  755. var rtpStats = null;
  756. var RTCService = null;
  757. function stopLocal()
  758. {
  759. if(localStats)
  760. {
  761. localStats.stop();
  762. localStats = null;
  763. }
  764. }
  765. function stopRemote()
  766. {
  767. if(rtpStats)
  768. {
  769. rtpStats.stop();
  770. eventEmitter.emit("statistics.stop");
  771. rtpStats = null;
  772. }
  773. }
  774. function startRemoteStats (peerconnection) {
  775. if (config.enableRtpStats)
  776. {
  777. if(rtpStats)
  778. {
  779. rtpStats.stop();
  780. rtpStats = null;
  781. }
  782. rtpStats = new RTPStats(peerconnection, 200, 2000, eventEmitter);
  783. rtpStats.start();
  784. }
  785. }
  786. var statistics =
  787. {
  788. /**
  789. * Indicates that this audio level is for local jid.
  790. * @type {string}
  791. */
  792. LOCAL_JID: 'local',
  793. addAudioLevelListener: function(listener)
  794. {
  795. eventEmitter.on("statistics.audioLevel", listener);
  796. },
  797. removeAudioLevelListener: function(listener)
  798. {
  799. eventEmitter.removeListener("statistics.audioLevel", listener);
  800. },
  801. addConnectionStatsListener: function(listener)
  802. {
  803. eventEmitter.on("statistics.connectionstats", listener);
  804. },
  805. removeConnectionStatsListener: function(listener)
  806. {
  807. eventEmitter.removeListener("statistics.connectionstats", listener);
  808. },
  809. addRemoteStatsStopListener: function(listener)
  810. {
  811. eventEmitter.on("statistics.stop", listener);
  812. },
  813. removeRemoteStatsStopListener: function(listener)
  814. {
  815. eventEmitter.removeListener("statistics.stop", listener);
  816. },
  817. stop: function () {
  818. stopLocal();
  819. stopRemote();
  820. if(eventEmitter)
  821. {
  822. eventEmitter.removeAllListeners();
  823. }
  824. },
  825. stopRemoteStatistics: function()
  826. {
  827. stopRemote();
  828. },
  829. onConfereceCreated: function (event) {
  830. startRemoteStats(event.peerconnection);
  831. },
  832. onDisposeConference: function (onUnload) {
  833. stopRemote();
  834. if(onUnload) {
  835. stopLocal();
  836. eventEmitter.removeAllListeners();
  837. }
  838. },
  839. onStreamCreated: function(stream)
  840. {
  841. if(stream.getAudioTracks().length === 0)
  842. return;
  843. localStats = new LocalStats(stream, 100, this,
  844. eventEmitter);
  845. localStats.start();
  846. }
  847. };
  848. module.exports = statistics;
  849. },{"./LocalStatsCollector.js":1,"./RTPStatsCollector.js":2,"events":4}],4:[function(require,module,exports){
  850. // Copyright Joyent, Inc. and other Node contributors.
  851. //
  852. // Permission is hereby granted, free of charge, to any person obtaining a
  853. // copy of this software and associated documentation files (the
  854. // "Software"), to deal in the Software without restriction, including
  855. // without limitation the rights to use, copy, modify, merge, publish,
  856. // distribute, sublicense, and/or sell copies of the Software, and to permit
  857. // persons to whom the Software is furnished to do so, subject to the
  858. // following conditions:
  859. //
  860. // The above copyright notice and this permission notice shall be included
  861. // in all copies or substantial portions of the Software.
  862. //
  863. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  864. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  865. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  866. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  867. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  868. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  869. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  870. function EventEmitter() {
  871. this._events = this._events || {};
  872. this._maxListeners = this._maxListeners || undefined;
  873. }
  874. module.exports = EventEmitter;
  875. // Backwards-compat with node 0.10.x
  876. EventEmitter.EventEmitter = EventEmitter;
  877. EventEmitter.prototype._events = undefined;
  878. EventEmitter.prototype._maxListeners = undefined;
  879. // By default EventEmitters will print a warning if more than 10 listeners are
  880. // added to it. This is a useful default which helps finding memory leaks.
  881. EventEmitter.defaultMaxListeners = 10;
  882. // Obviously not all Emitters should be limited to 10. This function allows
  883. // that to be increased. Set to zero for unlimited.
  884. EventEmitter.prototype.setMaxListeners = function(n) {
  885. if (!isNumber(n) || n < 0 || isNaN(n))
  886. throw TypeError('n must be a positive number');
  887. this._maxListeners = n;
  888. return this;
  889. };
  890. EventEmitter.prototype.emit = function(type) {
  891. var er, handler, len, args, i, listeners;
  892. if (!this._events)
  893. this._events = {};
  894. // If there is no 'error' event listener then throw.
  895. if (type === 'error') {
  896. if (!this._events.error ||
  897. (isObject(this._events.error) && !this._events.error.length)) {
  898. er = arguments[1];
  899. if (er instanceof Error) {
  900. throw er; // Unhandled 'error' event
  901. } else {
  902. throw TypeError('Uncaught, unspecified "error" event.');
  903. }
  904. return false;
  905. }
  906. }
  907. handler = this._events[type];
  908. if (isUndefined(handler))
  909. return false;
  910. if (isFunction(handler)) {
  911. switch (arguments.length) {
  912. // fast cases
  913. case 1:
  914. handler.call(this);
  915. break;
  916. case 2:
  917. handler.call(this, arguments[1]);
  918. break;
  919. case 3:
  920. handler.call(this, arguments[1], arguments[2]);
  921. break;
  922. // slower
  923. default:
  924. len = arguments.length;
  925. args = new Array(len - 1);
  926. for (i = 1; i < len; i++)
  927. args[i - 1] = arguments[i];
  928. handler.apply(this, args);
  929. }
  930. } else if (isObject(handler)) {
  931. len = arguments.length;
  932. args = new Array(len - 1);
  933. for (i = 1; i < len; i++)
  934. args[i - 1] = arguments[i];
  935. listeners = handler.slice();
  936. len = listeners.length;
  937. for (i = 0; i < len; i++)
  938. listeners[i].apply(this, args);
  939. }
  940. return true;
  941. };
  942. EventEmitter.prototype.addListener = function(type, listener) {
  943. var m;
  944. if (!isFunction(listener))
  945. throw TypeError('listener must be a function');
  946. if (!this._events)
  947. this._events = {};
  948. // To avoid recursion in the case that type === "newListener"! Before
  949. // adding it to the listeners, first emit "newListener".
  950. if (this._events.newListener)
  951. this.emit('newListener', type,
  952. isFunction(listener.listener) ?
  953. listener.listener : listener);
  954. if (!this._events[type])
  955. // Optimize the case of one listener. Don't need the extra array object.
  956. this._events[type] = listener;
  957. else if (isObject(this._events[type]))
  958. // If we've already got an array, just append.
  959. this._events[type].push(listener);
  960. else
  961. // Adding the second element, need to change to array.
  962. this._events[type] = [this._events[type], listener];
  963. // Check for listener leak
  964. if (isObject(this._events[type]) && !this._events[type].warned) {
  965. var m;
  966. if (!isUndefined(this._maxListeners)) {
  967. m = this._maxListeners;
  968. } else {
  969. m = EventEmitter.defaultMaxListeners;
  970. }
  971. if (m && m > 0 && this._events[type].length > m) {
  972. this._events[type].warned = true;
  973. console.error('(node) warning: possible EventEmitter memory ' +
  974. 'leak detected. %d listeners added. ' +
  975. 'Use emitter.setMaxListeners() to increase limit.',
  976. this._events[type].length);
  977. if (typeof console.trace === 'function') {
  978. // not supported in IE 10
  979. console.trace();
  980. }
  981. }
  982. }
  983. return this;
  984. };
  985. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  986. EventEmitter.prototype.once = function(type, listener) {
  987. if (!isFunction(listener))
  988. throw TypeError('listener must be a function');
  989. var fired = false;
  990. function g() {
  991. this.removeListener(type, g);
  992. if (!fired) {
  993. fired = true;
  994. listener.apply(this, arguments);
  995. }
  996. }
  997. g.listener = listener;
  998. this.on(type, g);
  999. return this;
  1000. };
  1001. // emits a 'removeListener' event iff the listener was removed
  1002. EventEmitter.prototype.removeListener = function(type, listener) {
  1003. var list, position, length, i;
  1004. if (!isFunction(listener))
  1005. throw TypeError('listener must be a function');
  1006. if (!this._events || !this._events[type])
  1007. return this;
  1008. list = this._events[type];
  1009. length = list.length;
  1010. position = -1;
  1011. if (list === listener ||
  1012. (isFunction(list.listener) && list.listener === listener)) {
  1013. delete this._events[type];
  1014. if (this._events.removeListener)
  1015. this.emit('removeListener', type, listener);
  1016. } else if (isObject(list)) {
  1017. for (i = length; i-- > 0;) {
  1018. if (list[i] === listener ||
  1019. (list[i].listener && list[i].listener === listener)) {
  1020. position = i;
  1021. break;
  1022. }
  1023. }
  1024. if (position < 0)
  1025. return this;
  1026. if (list.length === 1) {
  1027. list.length = 0;
  1028. delete this._events[type];
  1029. } else {
  1030. list.splice(position, 1);
  1031. }
  1032. if (this._events.removeListener)
  1033. this.emit('removeListener', type, listener);
  1034. }
  1035. return this;
  1036. };
  1037. EventEmitter.prototype.removeAllListeners = function(type) {
  1038. var key, listeners;
  1039. if (!this._events)
  1040. return this;
  1041. // not listening for removeListener, no need to emit
  1042. if (!this._events.removeListener) {
  1043. if (arguments.length === 0)
  1044. this._events = {};
  1045. else if (this._events[type])
  1046. delete this._events[type];
  1047. return this;
  1048. }
  1049. // emit removeListener for all listeners on all events
  1050. if (arguments.length === 0) {
  1051. for (key in this._events) {
  1052. if (key === 'removeListener') continue;
  1053. this.removeAllListeners(key);
  1054. }
  1055. this.removeAllListeners('removeListener');
  1056. this._events = {};
  1057. return this;
  1058. }
  1059. listeners = this._events[type];
  1060. if (isFunction(listeners)) {
  1061. this.removeListener(type, listeners);
  1062. } else {
  1063. // LIFO order
  1064. while (listeners.length)
  1065. this.removeListener(type, listeners[listeners.length - 1]);
  1066. }
  1067. delete this._events[type];
  1068. return this;
  1069. };
  1070. EventEmitter.prototype.listeners = function(type) {
  1071. var ret;
  1072. if (!this._events || !this._events[type])
  1073. ret = [];
  1074. else if (isFunction(this._events[type]))
  1075. ret = [this._events[type]];
  1076. else
  1077. ret = this._events[type].slice();
  1078. return ret;
  1079. };
  1080. EventEmitter.listenerCount = function(emitter, type) {
  1081. var ret;
  1082. if (!emitter._events || !emitter._events[type])
  1083. ret = 0;
  1084. else if (isFunction(emitter._events[type]))
  1085. ret = 1;
  1086. else
  1087. ret = emitter._events[type].length;
  1088. return ret;
  1089. };
  1090. function isFunction(arg) {
  1091. return typeof arg === 'function';
  1092. }
  1093. function isNumber(arg) {
  1094. return typeof arg === 'number';
  1095. }
  1096. function isObject(arg) {
  1097. return typeof arg === 'object' && arg !== null;
  1098. }
  1099. function isUndefined(arg) {
  1100. return arg === void 0;
  1101. }
  1102. },{}]},{},[3])(3)
  1103. });