-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.js
258 lines (241 loc) · 9.54 KB
/
client.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
$(document).ready(function() {
// grab the room from the URL
var room = location.search && location.search.split('?')[1];
// create our webrtc connection
var webrtc = new SimpleWebRTC({
// the id/element dom element that will hold "our" video
localVideoEl: 'localVideo',
// the id/element dom element that will hold remote videos
remoteVideosEl: '',
// immediately ask for camera access
autoRequestMedia: true,
debug: false
});
// when it's ready, join if we got a room from the URL
webrtc.on('readyToCall', function () {
if (room) webrtc.joinRoom(room);
window.callInfo = [];
});
webrtc.on('channelMessage', function (peer, label, data) {
if (data.type == 'volume') {
showVolume(document.getElementById('volume_' + peer.id), data.volume);
}
});
//videoAdded event. peer video has been added
webrtc.on('videoAdded', function (video, peer) {
console.log('video added', peer);
var remotes = document.getElementById('remotes');
if (remotes) {
var d = document.createElement('div');
d.className = 'videoContainer';
d.id = 'container_' + webrtc.getDomId(peer);
d.appendChild(video);
var vol = document.createElement('div');
vol.id = 'volume_' + peer.id;
vol.className = 'volume_bar';
video.onclick = function () {
video.style.width = video.videoWidth + 'px';
video.style.height = video.videoHeight + 'px';
};
d.appendChild(vol);
// start timing immediately when the call begins.
if (peer && peer.pc) {
window.getStatsTimer = setInterval(function() {
peer.pc.getStats(function(err, stats) {
var test = {};
test.results = $.map(stats, function(value, index) {
return [value];
});
window.callInfo.push(test); // push unprocessed for now to not slow things down
});
}, 1000);
var connstate = document.createElement('div');
connstate.className = 'connection-state';
peer.pc.on('iceConnectionStateChange', function(e) {
switch (peer.pc.iceConnectionState) {
case 'checking':
connstate.innerText = 'Connecting to peer...';
break;
case 'connected':
case 'completed': //on caller side
connstate.innerText = 'Connection established.';
break;
case 'disconnected':
connstate.innerText = 'Disconnected';
case 'failed':
connstate.innerText = 'Connection failed';
case 'closed':
connstate.innerText = 'Connection closed.';
break;
}
});
}
remotes.appendChild(d);
}
});
// a peer was removed
webrtc.on('videoRemoved', function (video, peer) {
clearInterval(window.getStatsTimer);
console.log('video removed ', peer);
var remotes = document.getElementById('remotes');
var el = document.getElementById('container_' + webrtc.getDomId(peer));
if (remotes && el) {
remotes.removeChild(el);
}
});
webrtc.on('volumeChange', function (volume, treshold) {
showVolume(document.getElementById('localVolume'), volume);
});
// Since we use this twice we put it here
function setRoom(name) {
$('form').remove();
$('h3').text(name);
$('#subTitle').text('Link to join: ' + location.href);
$('body').addClass('active');
}
if (room) {
setRoom(room);
} else {
$('form').submit(function () {
var val = $('#sessionInput').val().toLowerCase().replace(/\s/g, '-').replace(/[^A-Za-z0-9_\-]/g, '');
webrtc.createRoom(val, function (err, name) {
console.log(' create room cb', arguments);
var newUrl = location.pathname + '?' + name;
if (!err) {
history.replaceState({foo: 'bar'}, null, newUrl);
setRoom(name);
} else {
console.log(err);
}
});
return false;
});
}
var statsButton = $('#logStats');
statsButton.text('log stats');
statsButton.click(function() {
console.log('preparing to upload');
uploadData();
});
function getNecessaryData(res){
var result = {};
result.sentAudio = {};
result.sentVideo = {};
result.recvVideo = {};
result.recvAudio = {};
for(var i = 0; i < res.results.length; i++) {
switch (res.results[i].type) {
case 'ssrc':
if(res.results[i].audioInputLevel && res.results[i].id.indexOf('send') !== -1) {
result.sentAudio = {};
result.sentAudio.googCodecName = res.results[i].googCodecName;
result.sentAudio.audioInputLevel = res.results[i].audioInputLevel;
result.sentAudio.bytesSent = res.results[i].bytesSent;
result.sentAudio.googJitterReceived = res.results[i].googJitterReceived;
result.sentAudio.googRtt = res.results[i].googRtt;
result.sentAudio.packetsLost = res.results[i].packetsLost;
result.sentAudio.packetsSent = res.results[i].packetsSent;
result.sentAudio.timestamp = res.results[i].timestamp;
}
else if(res.results[i].googFrameHeightSent && res.results[i].id.indexOf('send') !== -1) {
result.sentVideo = {};
result.sentVideo.googCodecName = res.results[i].googCodecName;
result.sentVideo.bytesSent = res.results[i].bytesSent;
result.sentVideo.googAdaptationChanges = res.results[i].googAdaptationChanges;
result.sentVideo.googBandwidthLimitedResolution = res.results[i].googBandwidthLimitedResolution;
result.sentVideo.googViewLimitedResolution = res.results[i].googViewLimitedResolution;
result.sentVideo.googCpuLimitedResolution = res.results[i].googCpuLimitedResolution;
result.sentVideo.googAvgEncodeMs = res.results[i].googAvgEncodeMs;
result.sentVideo.googEncodeUsagePercent = res.results[i].googEncodeUsagePercent;
result.sentVideo.googFrameHeightInput = res.results[i].googFrameHeightInput;
result.sentVideo.googFrameHeightSent = res.results[i].googFrameHeightSent;
result.sentVideo.googFrameRateInput = res.results[i].googFrameRateInput;
result.sentVideo.googFrameRateSent = res.results[i].googFrameRateSent;
result.sentVideo.googFrameWidthInput = res.results[i].googFrameWidthInput;
result.sentVideo.googFrameWidthSent = res.results[i].googFrameWidthSent;
result.sentVideo.googNacksReceived = res.results[i].googNacksReceived;
result.sentVideo.googPlisReceived = res.results[i].googPlisReceived;
result.sentVideo.googRtt = res.results[i].googRtt;
result.sentVideo.packetsLost = res.results[i].packetsLost;
result.sentVideo.packetsSent = res.results[i].packetsSent;
result.sentVideo.timestamp = res.results[i].timestamp;
} else if(res.results[i].audioOutputLevel && res.results[i].id.indexOf('recv') !== -1) {
result.recvAudio = {};
result.recvAudio.audioOutputLevel = res.results[i].audioOutputLevel;
result.recvAudio.googCodecName = res.results[i].googCodecName;
result.recvAudio.bytesReceived = res.results[i].bytesReceived;
result.recvAudio.googCurrentDelayMs = res.results[i].googCurrentDelayMs;
result.recvAudio.packetsLost = res.results[i].packetsLost;
result.recvAudio.packetsReceived = res.results[i].packetsReceived;
result.recvAudio.timestamp = res.results[i].timestamp;
} else if(res.results[i].googFrameHeightReceived && res.results[i].id.indexOf('recv') !== -1) {
result.recvVideo = {};
result.recvVideo.googCodecName = res.results[i].googCodecName;
result.recvVideo.bytesReceived = res.results[i].bytesReceived;
result.recvVideo.googCurrentDelayMs = res.results[i].googCurrentDelayMs;
result.recvVideo.googDecodeMs = res.results[i].googDecodeMs;
result.recvVideo.googFrameRateReceived = res.results[i].googFrameRateReceived;
result.recvVideo.googFrameHeightReceived = res.results[i].googFrameHeightReceived;
result.recvVideo.googFrameWidthReceived = res.results[i].googFrameWidthReceived;
result.recvVideo.googFrameRateOutput = res.results[i].googFrameRateOutput;
result.recvVideo.googNacksSent = res.results[i].googNacksSent;
result.recvVideo.googPlisSent = res.results[i].googPlisSent;
result.recvVideo.packetsLost = res.results[i].packetsLost;
result.recvVideo.packetsReceived = res.results[i].packetsReceived;
result.recvVideo.timestamp = res.results[i].timestamp;
}
break;
case 'VideoBwe':
result.videoBWE = res.results[i];
break;
case 'googCandidatePair':
if (res.results[i].googActiveConnection === "true") {
result.bytesReceived = res.results[i].bytesReceived;
result.bytesSent = res.results[i].bytesSent;
result.googRtt = res.results[i].googRtt;
result.packetDiscardedOnSend = res.results[i].packetsDiscardedOnSend;
result.packetsSent = res.results[i].packetsSent;
result.timestamp = res.results[i].timestamp;
}
}
}
return result;
}
function fmtr(nr) {
if (nr < 10){
nr = '0' + nr;
}
return nr;
}
function getTime() {
var now = new Date();
var year = fmtr(now.getFullYear());
var month = fmtr(now.getMonth() + 1);
var day = fmtr(now.getDate());
var hours = fmtr(now.getHours());
var minutes = fmtr(now.getMinutes());
var seconds = fmtr(now.getSeconds());
return year + '-' + month + '-' + day + '-' + hours + ':' + minutes + ':' + seconds;
}
function uploadData() {
var data = [];
if (!window.callInfo || window.callInfo.length === 0) {
console.error('no callInfo to upload, exiting!');
return;
}
// set up database connection here
var db = firebase.database();
// process raw data from callStats() API into the model we want to use
for(var i = 0; i < window.callInfo.length; i++) {
data.push(getNecessaryData(window.callInfo[i]));
}
var json_resp = JSON.stringify(data);
db.ref('/logs').push({
date: getTime(),
contents: json_resp,
callLength: window.callInfo.length
}, function() {
alert('successfully pushed data to firebase');
});
}
});