-
Notifications
You must be signed in to change notification settings - Fork 44
/
extract.js
216 lines (204 loc) · 9.02 KB
/
extract.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const fs = require('fs');
const canUseProcessSend = !!process.send;
const isProduction = process.env.NODE_ENV && process.env.NODE_ENV === 'production';
function capitalize(str) {
return str[0].toUpperCase() + str.substr(1);
}
function safeFeature(feature) {
if (typeof feature === 'number' && isNaN(feature)) feature = -1;
if (typeof feature === 'number' && !isFinite(feature)) feature = -2;
/*
if (feature === false) feature = 0;
if (feature === true) feature = 1;
*/
return feature;
}
const connectionfeatures = require('./features-connection');
const clientfeatures = require('./features-client');
const streamfeatures = require('./features-stream');
const statsDecompressor = require('./getstats-deltacompression').decompress;
const statsMangler = require('./getstats-mangle');
const {extractTracks, extractStreams} = require('./utils');
// dumps all peerconnections.
function dump(url, client) {
// ignore connections that never send getUserMedia or peerconnection events.
if (client.getUserMedia.length === 0 && Object.keys(client.peerConnections).length === 0) return;
if (!isProduction) {
let total = 0;
Object.keys(client.peerConnections).forEach(id => {
total += client.peerConnections[id].length;
});
console.log('DUMP', client.getUserMedia.length, Object.keys(client.peerConnections).length, total);
return;
}
}
// Feature generation
function generateFeatures(url, client, clientid) {
// ignore connections that never send getUserMedia or peerconnection events.
if (client.getUserMedia.length === 0 && Object.keys(client.peerConnections).length === 0) return;
// clientFeatures are the same for all peerconnections but are saved together
// with each peerconnection anyway to make correlation easier.
const clientFeatures = {};
Object.keys(clientfeatures).forEach(fname => {
let feature = clientfeatures[fname].apply(null, [client]);
if (feature !== undefined) {
if (typeof feature === 'object') {
Object.keys(feature).forEach(subname => {
feature[subname] = safeFeature(feature[subname]);
if (!isProduction) {
console.log('PAGE', 'FEATURE', fname + capitalize(subname), '=>', safeFeature(feature[subname]));
}
clientFeatures[fname + capitalize(subname)] = feature[subname];
});
} else {
feature = safeFeature(feature);
if (!isProduction) {
console.log('PAGE', 'FEATURE', fname, '=>', feature);
}
clientFeatures[fname] = feature;
}
}
});
if (Object.keys(client.peerConnections).length === 0) {
// we only have GUM and potentially GUM errors.
if (canUseProcessSend && isProduction) {
process.send({url, clientid, connid: '', clientFeatures});
}
}
Object.keys(client.peerConnections).forEach(connid => {
if (connid === 'null' || connid === '') return; // ignore the null connid and empty strings
const conn = client.peerConnections[connid];
const connectionFeatures = {};
Object.keys(connectionfeatures).forEach(fname => {
let feature = connectionfeatures[fname].apply(null, [client, conn]);
if (feature !== undefined) {
if (typeof feature === 'object') {
Object.keys(feature).forEach(subname => {
feature[subname] = safeFeature(feature[subname]);
if (!isProduction) {
console.log(connid, 'FEATURE', fname + capitalize(subname), '=>', safeFeature(feature[subname]));
}
connectionFeatures[fname + capitalize(subname)] = feature[subname];
});
} else {
feature = safeFeature(feature);
if (!isProduction) {
console.log(connid, 'FEATURE', fname, '=>', safeFeature(feature));
}
connectionFeatures[fname] = feature;
}
}
});
const tracks = extractTracks(conn);
const streams = extractStreams(tracks);
for (const [streamId, tracks] of streams.entries()) {
const streamFeatures = {streamId};
for (const {trackId, kind, direction, stats} of tracks) {
Object.keys(streamfeatures).forEach(fname => {
let feature = streamfeatures[fname].apply(null, [{kind, direction, trackId, stats, peerConnectionLog: conn}]);
if (feature !== undefined) {
feature = safeFeature(feature);
if (typeof feature === 'object') {
Object.keys(feature).forEach(subname => {
feature[subname] = safeFeature(feature[subname]);
if (!isProduction) {
console.log(connid, 'STREAM', streamId, 'TRACK', trackId, 'FEATURE', fname + capitalize(subname), '=>', safeFeature(feature[subname]));
}
streamFeatures[fname + capitalize(subname)] = feature[subname];
});
} else {
feature = safeFeature(feature);
if (!isProduction) {
console.log(connid, 'STREAM', streamId, 'TRACK', trackId, 'FEATURE', fname, '=>', safeFeature(feature));
}
streamFeatures[fname] = feature;
}
}
});
}
if (canUseProcessSend && isProduction) {
process.send({url, clientid, connid, clientFeatures, connectionFeatures, streamFeatures});
}
}
delete client.peerConnections[connid]; // save memory
});
}
const clientid = process.argv[2];
const path = 'temp/' + clientid;
fs.readFile(path, {encoding: 'utf-8'}, (err, data) => {
if (err) {
console.error(err, path);
return;
}
const baseStats = {};
const lines = data.split('\n');
const client = JSON.parse(lines.shift());
client.peerConnections = {};
client.getUserMedia = [];
lines.forEach(line => {
if (line.length) {
const data = JSON.parse(line);
const time = new Date(data.time || data[3]);
delete data.time;
switch(data[0]) {
case 'publicIP':
client.publicIP = data[2];
break;
case 'userfeedback': // TODO: might be renamed
client.feedback = data[2];
break;
case 'tags': // experiment variation tags
client.tags = data[2];
break;
case 'wsconnect':
client.websocketConnectionTime = data[2] >>> 0;
break;
case 'wsconnecterror':
client.websocketError = data[2];
break;
case 'identity': // identity meta-information when its not possible to feed into RTCPeerConnection.
client.identity = data[2];
break;
case 'getUserMedia':
case 'getUserMediaOnSuccess':
case 'getUserMediaOnFailure':
case 'navigator.mediaDevices.getUserMedia':
case 'navigator.mediaDevices.getUserMediaOnSuccess':
case 'navigator.mediaDevices.getUserMediaOnFailure':
case 'navigator.getDisplayMedia':
case 'navigator.getDisplayMediaOnSucces':
case 'navigator.mediaDevices.getDisplayMedia':
case 'navigator.mediaDevices.getDisplayMediaOnSuccess':
client.getUserMedia.push({
time: time,
timestamp: time.getTime(),
type: data[0],
value: data[2]
});
break;
default:
if (!client.peerConnections[data[1]]) {
client.peerConnections[data[1]] = [];
baseStats[data[1]] = {};
}
if (data[0] === 'getstats') { // delta-compressed
data[2] = statsDecompressor(baseStats[data[1]], data[2]);
baseStats[data[1]] = JSON.parse(JSON.stringify(data[2]));
}
if (data[0] === 'getStats' || data[0] === 'getstats') {
data[2] = statsMangler(data[2]);
data[0] = 'getStats';
}
client.peerConnections[data[1]].push({
time: time,
timestamp: time.getTime(),
type: data[0],
value: data[2],
});
break;
}
}
});
dump(client.url, client);
generateFeatures(client.url, client, clientid);
});