|
@@ -1,4 +1,5 @@
|
1
|
1
|
/* global ssrc2jid */
|
|
2
|
+/* jshint -W117 */
|
2
|
3
|
/**
|
3
|
4
|
* Calculates packet lost percent using the number of lost packets and the
|
4
|
5
|
* number of all packet.
|
|
@@ -125,6 +126,37 @@ function StatsCollector(peerconnection, audioLevelsInterval,
|
125
|
126
|
this.currentStatsReport = null;
|
126
|
127
|
this.baselineStatsReport = null;
|
127
|
128
|
this.audioLevelsIntervalId = null;
|
|
129
|
+
|
|
130
|
+ /**
|
|
131
|
+ * Gather PeerConnection stats once every this many milliseconds.
|
|
132
|
+ */
|
|
133
|
+ this.GATHER_INTERVAL = 10000;
|
|
134
|
+
|
|
135
|
+ /**
|
|
136
|
+ * Log stats via the focus once every this many milliseconds.
|
|
137
|
+ */
|
|
138
|
+ this.LOG_INTERVAL = 60000;
|
|
139
|
+
|
|
140
|
+ /**
|
|
141
|
+ * Gather stats and store them in this.statsToBeLogged.
|
|
142
|
+ */
|
|
143
|
+ this.gatherStatsIntervalId = null;
|
|
144
|
+
|
|
145
|
+ /**
|
|
146
|
+ * Send the stats already saved in this.statsToBeLogged to be logged via
|
|
147
|
+ * the focus.
|
|
148
|
+ */
|
|
149
|
+ this.logStatsIntervalId = null;
|
|
150
|
+
|
|
151
|
+ /**
|
|
152
|
+ * Stores the statistics which will be send to the focus to be logged.
|
|
153
|
+ */
|
|
154
|
+ this.statsToBeLogged =
|
|
155
|
+ {
|
|
156
|
+ timestamps: [],
|
|
157
|
+ stats: {}
|
|
158
|
+ };
|
|
159
|
+
|
128
|
160
|
// Updates stats interval
|
129
|
161
|
this.audioLevelsIntervalMilis = audioLevelsInterval;
|
130
|
162
|
|
|
@@ -148,6 +180,10 @@ StatsCollector.prototype.stop = function ()
|
148
|
180
|
this.audioLevelsIntervalId = null;
|
149
|
181
|
clearInterval(this.statsIntervalId);
|
150
|
182
|
this.statsIntervalId = null;
|
|
183
|
+ clearInterval(this.logStatsIntervalId);
|
|
184
|
+ this.logStatsIntervalId = null;
|
|
185
|
+ clearInterval(this.gatherStatsIntervalId);
|
|
186
|
+ this.gatherStatsIntervalId = null;
|
151
|
187
|
}
|
152
|
188
|
};
|
153
|
189
|
|
|
@@ -204,8 +240,81 @@ StatsCollector.prototype.start = function ()
|
204
|
240
|
},
|
205
|
241
|
self.statsIntervalMilis
|
206
|
242
|
);
|
|
243
|
+
|
|
244
|
+ if (config.logStats) {
|
|
245
|
+ this.gatherStatsIntervalId = setInterval(
|
|
246
|
+ function () {
|
|
247
|
+ self.peerconnection.getStats(
|
|
248
|
+ function (report) {
|
|
249
|
+ self.addStatsToBeLogged(report.result());
|
|
250
|
+ },
|
|
251
|
+ function () {
|
|
252
|
+ }
|
|
253
|
+ );
|
|
254
|
+ },
|
|
255
|
+ this.GATHER_INTERVAL
|
|
256
|
+ );
|
|
257
|
+
|
|
258
|
+ this.logStatsIntervalId = setInterval(
|
|
259
|
+ function() { self.logStats(); },
|
|
260
|
+ this.LOG_INTERVAL);
|
|
261
|
+ }
|
207
|
262
|
};
|
208
|
263
|
|
|
264
|
+/**
|
|
265
|
+ * Converts the stats to the format used for logging, and saves the data in
|
|
266
|
+ * this.statsToBeLogged.
|
|
267
|
+ * @param reports Reports as given by webkitRTCPerConnection.getStats.
|
|
268
|
+ */
|
|
269
|
+StatsCollector.prototype.addStatsToBeLogged = function (reports) {
|
|
270
|
+ var self = this;
|
|
271
|
+ var num_records = this.statsToBeLogged.timestamps.length;
|
|
272
|
+ this.statsToBeLogged.timestamps.push(new Date().getTime());
|
|
273
|
+ reports.map(function (report) {
|
|
274
|
+ var stat = self.statsToBeLogged.stats[report.id];
|
|
275
|
+ if (!stat) {
|
|
276
|
+ stat = self.statsToBeLogged.stats[report.id] = {};
|
|
277
|
+ }
|
|
278
|
+ stat.type = report.type;
|
|
279
|
+ report.names().map(function (name) {
|
|
280
|
+ var values = stat[name];
|
|
281
|
+ if (!values) {
|
|
282
|
+ values = stat[name] = [];
|
|
283
|
+ }
|
|
284
|
+ while (values.length < num_records) {
|
|
285
|
+ values.push(null);
|
|
286
|
+ }
|
|
287
|
+ values.push(report.stat(name));
|
|
288
|
+ });
|
|
289
|
+ });
|
|
290
|
+};
|
|
291
|
+
|
|
292
|
+StatsCollector.prototype.logStats = function () {
|
|
293
|
+ var deflate = true;
|
|
294
|
+
|
|
295
|
+ var content = JSON.stringify(this.statsToBeLogged);
|
|
296
|
+ if (deflate) {
|
|
297
|
+ content = String.fromCharCode.apply(null, Pako.deflateRaw(content));
|
|
298
|
+ }
|
|
299
|
+ content = Base64.encode(content);
|
|
300
|
+
|
|
301
|
+ // XEP-0337-ish
|
|
302
|
+ var message = $msg();
|
|
303
|
+ message.c('log', { xmlns: 'urn:xmpp:eventlog',
|
|
304
|
+ id: 'PeerConnectionStats'});
|
|
305
|
+ message.c('message').t(content).up();
|
|
306
|
+ if (deflate) {
|
|
307
|
+ message.c('tag', {name: "deflated", value: "true"}).up();
|
|
308
|
+ }
|
|
309
|
+ message.up();
|
|
310
|
+
|
|
311
|
+ //TODO: actually send the message...
|
|
312
|
+ console.log(message.toString());
|
|
313
|
+
|
|
314
|
+ // Reset the stats
|
|
315
|
+ this.statsToBeLogged.stats = {};
|
|
316
|
+ this.statsToBeLogged.timestamps = [];
|
|
317
|
+};
|
209
|
318
|
|
210
|
319
|
/**
|
211
|
320
|
* Stats processing logic.
|