-
Notifications
You must be signed in to change notification settings - Fork 44
/
features.js
1753 lines (1629 loc) · 73.2 KB
/
features.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
// https://en.wikipedia.org/wiki/Feature_extraction for peerconnection
// API traces and getStats data.
var fs = require('fs');
var platform = require('platform');
function capitalize(str) {
return str[0].toUpperCase() + str.substr(1);
}
function getPeerConnectionConfig(peerConnectionLog) {
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'create') {
return peerConnectionLog[i].value;
}
}
}
function getPeerConnectionConstraints(peerConnectionLog) {
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'constraints') {
return peerConnectionLog[i].value;
}
}
}
function determineBrowserFromOLine(sdp) {
if (sdp.indexOf('v=0\r\no=mozilla...THIS_IS_SDPARTA') === 0) {
return 'moz';
} else if (sdp.indexOf('v=0\r\no=thisisadapterortc') === 0) {
return 'edge';
} else if (sdp.indexOf('a=msid-semantic: WMS APPEAR\r\n') === 0) {
return 'appear.in mobile';
} else {
return 'webrtc.org'; // maybe?
}
}
function gatheringTimeTURN(protocol, client, peerConnectionLog) {
var peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
var typepref;
switch(peerConnectionConfig.browserType) {
case 'webkit':
typepref = {
udp: 2,
tcp: 1,
tls: 0
}[protocol];
break;
case 'moz':
typepref = {
udp: 5,
tcp: 0
}[protocol];
break;
default:
typepref = 'unknown';
break;
}
var first;
var second;
for (first = 0; first < peerConnectionLog.length; first++) {
// TODO: is setLocalDescriptionOnSuccess better?
if (peerConnectionLog[first].type === 'setLocalDescription') break;
}
if (first < peerConnectionLog.length) {
for (second = first + 1; second < peerConnectionLog.length; second++) {
if (peerConnectionLog[second].type === 'onicecandidate') {
var cand = peerConnectionLog[second].value;
if (cand === null) return; // give up
if (cand && cand.candidate.indexOf('relay') !== -1) {
var localTypePref = cand.candidate.split(' ')[3] >> 24;
if (localTypePref === typepref) {
break;
}
}
}
}
if (second < peerConnectionLog.length) {
return (new Date(peerConnectionLog[second].time).getTime() -
new Date(peerConnectionLog[first].time).getTime());
}
}
}
function extractLastVideoStat(peerConnectionLog, type) {
var statsReport;
for (var i = peerConnectionLog.length - 1; i >= 0; i--) {
if (peerConnectionLog[i].type === 'getStats') {
statsReport = peerConnectionLog[i].value;
break;
}
}
if (!statsReport) return;
var count;
Object.keys(statsReport).forEach(function(id) {
// type outboundrtp && mediaType video
var report = statsReport[id];
if (report.type === 'outboundrtp' && report.mediaType === 'video') {
count = report[type];
}
});
return count;
}
// determine mode (most common) element in a series.
function mode(series) {
var modes = {};
series.forEach(function(item) {
if (!modes[item]) modes[item] = 0;
modes[item]++;
});
var value = -1;
var max = -1;
Object.keys(modes).forEach(function(key) {
if (modes[key] > max) {
max = modes[key];
value = key;
}
});
return value;
}
// Calculate standardized moment.
// order=1: 0
// order=2: variance
// order=3: skewness
// order=4: kurtosis
function standardizedMoment(series, order) {
var len = series.length || 1;
var mean = series.reduce(function(a, b) { return a + b; }, 0) / len;
return series.reduce(function(a, b) { return a + Math.pow(b - mean, order); }, 0) / len;
}
// extracts the central moment from video statistics.
function extractCentralMomentFromSsrcStat(peerConnectionLog, statName, order, mediaType) {
var series = [];
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'getStats') {
var statsReport = peerConnectionLog[i].value;
Object.keys(statsReport).forEach(function(id) {
var report = statsReport[id];
if (report.type === 'ssrc' && report.mediaType === mediaType && report[statName]) {
series.push(parseInt(report[statName], 10));
}
});
}
}
return series.length ? standardizedMoment(series, order) : undefined;
}
function extractCentralMomentFromVideoStat(peerConnectionLog, statName, order) {
return extractCentralMomentFromSsrcStat(peerConnectionLog, statName, order, 'video');
}
function extractCentralMomentFromAudioStat(peerConnectionLog, statName, order) {
return extractCentralMomentFromSsrcStat(peerConnectionLog, statName, order, 'audio');
}
// extract the codec used. Has to happen after the connection is up and packets have
// been received or sent.
function getCodec(peerConnectionLog, mediaType, direction) {
var codecName;
var connected = false;
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'oniceconnectionstatechange') {
if (peerConnectionLog[i].value === 'connected' || peerConnectionLog[i].value === 'completed') {
connected = true;
}
}
if (!connected) continue;
if (peerConnectionLog[i].type !== 'getStats') continue;
var statsReport = peerConnectionLog[i].value;
Object.keys(statsReport).forEach(function(id) {
var report = statsReport[id];
if (report.type === 'ssrc' && report.mediaType === mediaType &&
report.googCodecName && report.googCodecName.length
&& id.indexOf(direction) !== -1) {
codecName = report.googCodecName;
}
});
if (codecName) return codecName;
}
}
// extract a local/remote audio or video track.
function extractTrack(peerConnectionLog, kind, direction) {
var trackId;
var reports = [];
var streamevent = 'onaddstream';
if (direction === 'send') {
streamevent = 'addStream';
}
// search for the (first) track of that kind.
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === streamevent) {
var tracks = peerConnectionLog[i].value.split(' ', 2);
tracks.shift();
tracks = tracks[0].split(',');
for (var j = 0; j < tracks.length; j++) {
if (tracks[j].split(':')[0] === kind) {
trackId = tracks[j].split(':')[1];
break;
}
}
if (trackId) break;
}
}
if (!trackId) return []; // bail out
// search for signs of that track
for (; i < peerConnectionLog.length; i++) {
if (trackId && peerConnectionLog[i].type === 'getStats') {
var statsReport = peerConnectionLog[i].value;
Object.keys(statsReport).forEach(function(id) {
var report = statsReport[id];
if (report.type === 'ssrc' && report.trackIdentifier === trackId) {
report.timestamp = peerConnectionLog[i].time;
reports.push(report);
}
});
}
}
return reports;
}
function extractBWE(peerConnectionLog) {
var reports = [];
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'getStats') {
var statsReport = peerConnectionLog[i].value;
Object.keys(statsReport).forEach(function(id) {
var report = statsReport[id];
if (report.type === 'VideoBwe') {
reports.push(report);
}
});
}
}
return reports;
}
// there are two types of features
// 1) features which only take the client as argument. E.g. extracting the browser version
// 2) features which take the client and a connection argument. Those do something with the connection.
module.exports = {
origin: function(client) {
return client.origin;
},
browser: function(client) {
if (!(client.userAgent && client.userAgent.length)) return;
var ua = platform.parse(client.userAgent);
var parts = {
name: ua.name || 'unknown',
version: ua.version || '-1',
os: ua.os.toString(),
userAgent: client.userAgent,
nameVersion: ua.name + '/' + ua.version,
nameOs: ua.name + '/' + ua.os.toString(),
nameVersionOs: ua.name + '/' + ua.version + '/' + ua.os.toString()
};
if (ua.version) {
parts.majorVersion = ua.version.split('.')[0];
}
return parts;
},
// did the page call getUserMedia at all?
calledGetUserMedia: function(client) {
return client.getUserMedia && client.getUserMedia.length > 0;
},
location: function(client) {
if (!client.location) return;
var location = client.location;
return {
lon: location.location.longitude,
lat: location.location.latitude,
lonLat: JSON.stringify([location.location.longitude, location.location.latitude]),
continent: location.continent.code,
country: location.country ? location.country.names.en : 'unknown county',
city: location.city ? location.city.names.en : 'unknown city'
};
},
// did the page use the old getUserMedia?
calledLegacyGetUserMedia: function(client) {
var gum = client.getUserMedia || [];
for (var i = 0; i < gum.length; i++) {
if (gum[i].type === 'getUserMedia') return true;
}
return false;
},
// did the page use the new navigator.mediaDevices.getUserMedia?
calledMediadevicesGetUserMedia: function(client) {
var gum = client.getUserMedia || [];
for (var i = 0; i < gum.length; i++) {
if (gum[i].type === 'navigator.mediaDevices.getUserMedia') return true;
}
return false;
},
// TODO: was enumerateDevices used? rtcstats does not hook this and I do not think
// that tracing every call would be useful but enumerating hardware once might
// be nice for features like numberOfMicrophones, numberOfCameras, ...
// was there at least one getUserMedia success?
getUserMediaSuccess: function(client) {
var gum = client.getUserMedia || [];
for (var i = 0; i < gum.length; i++) {
if (gum[i].type === 'navigator.mediaDevices.getUserMediaOnSuccess' || gum[i].type === 'getUserMediaOnSuccess') {
return true;
}
}
return false;
},
// was there at least one getUserMedia error? If so, what was the error?
getUserMediaError: function(client) {
var gum = client.getUserMedia || [];
for (var i = 0; i < gum.length; i++) {
if (gum[i].type === 'navigator.mediaDevices.getUserMediaOnFailure' || gum[i].type === 'getUserMediaOnFailure') {
return gum[i].value;
}
}
return false;
},
// did the client ever request audio?
calledGetUserMediaRequestingAudio: function(client) {
var gum = client.getUserMedia || [];
var requested = false;
for (var i = 0; i < gum.length; i++) {
if (gum[i].type === 'navigator.mediaDevices.getUserMedia' || gum[i].type === 'getUserMedia') {
var options = gum[i].value;
if (options.audio && options.audio !== false) requested = true;
}
}
return requested;
},
// did the client ever request video (not screenshare)?
// screensharing is defined as
// mozMediaSource || mediaSource in FF (look for window || screen?)
// mandatory.chromeMediaSource: desktop in chrome
calledGetUserMediaRequestingVideo: function(client) {
var gum = client.getUserMedia || [];
var requested = false;
for (var i = 0; i < gum.length; i++) {
if (gum[i].type === 'navigator.mediaDevices.getUserMedia' || gum[i].type === 'getUserMedia') {
var options = gum[i].value;
if (options.video === true) {
requested = true;
break;
}
if (options.video && typeof options.video === 'object') {
if (!(options.video.mozMediaSource || options.video.mediaSource || options.video.chromeMediaSource)) {
requested = true;
break;
}
}
}
}
return requested;
},
// did the client ever request the screen?
// also returns the type even though (in chrome) that is not relevant.
calledGetUserMediaRequestingScreen: function(client) {
var gum = client.getUserMedia || [];
for (var i = 0; i < gum.length; i++) {
if (gum[i].type === 'navigator.mediaDevices.getUserMedia' || gum[i].type === 'getUserMedia') {
var options = gum[i].value;
if (options.video && typeof options.video === 'object') {
// Firefox
if (options.video.mozMediaSource || options.video.mediaSource) {
return options.video.mozMediaSource || options.video.mediaSource;
}
// Chrome
if (options.video.mandatory && options.video.mandatory.chromeMediaSource) {
return options.video.mandatory.chromeMediaSource;
}
}
}
}
return false;
},
timeBetweenGetUserMediaAndGetUserMediaSuccess: function(client) {
var gum = client.getUserMedia || [];
var first;
for (var i = 0; i < gum.length; i++) {
if (gum[i].type === 'navigator.mediaDevices.getUserMedia' || gum[i].type === 'getUserMedia') {
first = gum[i].time;
} else if (gum[i].type === 'navigator.mediaDevices.getUserMediaOnSuccess' || gum[i].type === 'getUserMediaOnSuccess') {
if (first) {
return new Date(gum[i].time).getTime() - new Date(first).getTime();
} else {
return -1;
}
}
}
},
timeBetweenGetUserMediaAndGetUserMediaFailure: function(client) {
var gum = client.getUserMedia || [];
var first;
for (var i = 0; i < gum.length; i++) {
if (gum[i].type === 'navigator.mediaDevices.getUserMedia' || gum[i].type === 'getUserMedia') {
first = gum[i].time;
} else if (gum[i].type === 'navigator.mediaDevices.getUserMediaOnFailure' || gum[i].type === 'getUserMediaOnFailure') {
if (first) {
return new Date(gum[i].time).getTime() - new Date(first).getTime();
} else {
return -1;
}
}
}
},
// return the label of the first audio device
firstAudioTrackLabel: function(client) {
var gum = client.getUserMedia || [];
for (var i = 0; i < gum.length; i++) {
if (gum[i].type === 'navigator.mediaDevices.getUserMediaOnSuccess' || gum[i].type === 'getUserMediaOnSuccess') {
var stream = gum[i].value;
var tracks = stream && stream.tracks || [];
for (var j = 0; j < tracks.length; j++) {
if (tracks[j].kind === 'audio') {
return tracks[j].label;
}
}
}
}
},
// return the label of the first video device
firstVideoTrackLabel: function(client) {
var gum = client.getUserMedia || [];
var gum = client.getUserMedia || [];
for (var i = 0; i < gum.length; i++) {
if (gum[i].type === 'navigator.mediaDevices.getUserMediaOnSuccess' || gum[i].type === 'getUserMediaOnSuccess') {
var stream = gum[i].value;
var tracks = stream && stream.tracks || [];
for (var j = 0; j < tracks.length; j++) {
if (tracks[j].kind === 'video') {
return tracks[j].label;
}
}
}
}
},
// TODO: gum statistics (audio, video, number of tracks, errors, fail-to-acquire aka ended readyState)
// TODO: resolution, framerate
// TODO: special goog constraints?
// TODO: feature for "were the promise-ified apis used or the legacy variants?"
// number of peerConnections created
numberOfPeerConnections: function(client) {
return Object.keys(client.peerConnections).length;
},
// client and conference identifiers, specified as optional peerconnection constraints
// (which are not a thing any longer). See https://github.com/opentok/rtcstats/issues/28
clientIdentifier: function(client, peerConnectionLog) {
var constraints = getPeerConnectionConstraints(peerConnectionLog) || [];
if (!constraints.optional) return;
constraints = constraints.optional;
for (var i = 0; i < constraints.length; i++) {
if (constraints[i].rtcStatsClientId) {
return constraints[i].rtcStatsClientId;
}
}
},
peerIdentifier: function(client, peerConnectionLog) {
var constraints = getPeerConnectionConstraints(peerConnectionLog) || [];
if (!constraints.optional) return;
constraints = constraints.optional;
for (var i = 0; i < constraints.length; i++) {
if (constraints[i].rtcStatsPeerId) {
return constraints[i].rtcStatsPeerId;
}
}
},
conferenceIdentifier: function(client, peerConnectionLog) {
var constraints = getPeerConnectionConstraints(peerConnectionLog) || [];
if (!constraints.optional) return;
constraints = constraints.optional;
for (var i = 0; i < constraints.length; i++) {
if (constraints[i].rtcStatsConferenceId) {
return constraints[i].rtcStatsConferenceId;
}
}
},
// when did the session start
startTime: function(client, peerConnectionLog) {
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'create') {
return new Date(peerConnectionLog[i].time).getTime();
}
}
},
// when did the session end
stopTime: function(client, peerConnectionLog) {
return new Date(peerConnectionLog[peerConnectionLog.length - 1].time).getTime();
},
// how long did the peerconnection live?
// not necessarily connected which is different from session duration
lifeTime: function(client, peerConnectionLog) {
return new Date(peerConnectionLog[peerConnectionLog.length - 1].time).getTime() - new Date(peerConnectionLog[0].time).getTime();
},
// the webrtc platform type -- webkit or moz
// TODO: edge, mobile platforms?
browserType: function(client, peerConnectionLog) {
var peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
return peerConnectionConfig.browserType || 'unknown';
},
// the remote platform, extracted from the remote description.
// only works for firefox and edge (using adapter)
// returns webrtc.org when unknown.
// TODO: look at chrome specifics?
remoteType: function(client, peerConnectionLog) {
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'setRemoteDescription') {
var sdp = peerConnectionLog[i].value.sdp;
return determineBrowserFromOLine(sdp);
}
}
},
// check if we are initiator/receiver (i.e. first called createOffer or createAnswer)
// this likely has implications for number and types of candidates gathered.
isInitiator: function(client, peerConnectionLog) {
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'createOffer') return true;
if (peerConnectionLog[i].type === 'setRemoteDescription') return false;
}
return undefined;
},
// was the peerconnection configured properly?
configured: function(client, peerConnectionLog) {
var peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
return peerConnectionConfig && peerConnectionConfig.nullConfig !== true;
},
// were ice servers configured? Not sure whether this is useful and/or should check if any empty list
// was configured
configuredWithICEServers: function(client, peerConnectionLog) {
var peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
return !!(peerConnectionConfig && peerConnectionConfig.iceServers !== undefined)
},
// was STUN configured in the peerconnection config?
configuredWithSTUN: function(client, peerConnectionLog) {
var peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
if (!(peerConnectionConfig && peerConnectionConfig.iceServers)) return;
for (var i = 0; i < peerConnectionConfig.iceServers.length; i++) {
var urls = peerConnectionConfig.iceServers[i].urls || [];
for (var j = 0; j < urls.length; j++) {
if (urls[j].indexOf('stun:') === 0) return true;
}
}
},
// was TURN (any kind) configured in the peerconnection config?
configuredWithTURN: function(client, peerConnectionLog) {
var peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
if (!(peerConnectionConfig && peerConnectionConfig.iceServers)) return;
for (var i = 0; i < peerConnectionConfig.iceServers.length; i++) {
var urls = peerConnectionConfig.iceServers[i].urls || [];
for (var j = 0; j < urls.length; j++) {
if (urls[j].indexOf('turn:') === 0 || urls[j].indexOf('turns:') === 0) return true;
}
}
},
// was TURN/UDP configured in the peerconnection config?
configuredWithTURNUDP: function(client, peerConnectionLog) {
var peerConnectionConfig = client.config;
if (!(peerConnectionConfig && peerConnectionConfig.iceServers)) return;
for (var i = 0; i < peerConnectionConfig.iceServers.length; i++) {
var urls = peerConnectionConfig.iceServers[i].urls || [];
for (var j = 0; j < urls.length; j++) {
if (urls[j].indexOf('turn:') === 0 && urls[j].indexOf('?transport=tcp') === -1) {
return true;
}
}
}
},
// was TURN/TCP configured in the peerconnection config?
configuredWithTURNTCP: function(client, peerConnectionLog) {
var peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
if (!(peerConnectionConfig && peerConnectionConfig.iceServers)) return;
for (var i = 0; i < peerConnectionConfig.iceServers.length; i++) {
var urls = peerConnectionConfig.iceServers[i].urls || [];
for (var j = 0; j < urls.length; j++) {
if (urls[j].indexOf('turn:') === 0 && urls[j].indexOf('?transport=tcp') !== -1) {
return true;
}
}
}
},
// was TURN/TLS configured in the peerconnection config?
// TODO: do we also want the port for this? does it make a difference whether turns is
// run on 443?
configuredWithTURNTLS: function(client, peerConnectionLog) {
var peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
if (!(peerConnectionConfig && peerConnectionConfig.iceServers)) return;
for (var i = 0; i < peerConnectionConfig.iceServers.length; i++) {
var urls = peerConnectionConfig.iceServers[i].urls || [];
for (var j = 0; j < urls.length; j++) {
if (urls[j].indexOf('turns:') === 0 && urls[j].indexOf('?transport=tcp') !== -1) {
return true;
}
}
}
},
// TODO: how long did it take to gather the respective candidates?
// we need to know the browsertype to figure out the correct local type preference
// since those differ in FF and Chrome
// what bundle policy was supplied?
// TODO: return default or do we want to measure explicit configuration?
configuredBundlePolicy: function(client, peerConnectionLog) {
var peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
return peerConnectionConfig ? peerConnectionConfig.bundlePolicy !== undefined : false; // default: 'balanced'
},
// what rtcp-mux configuration was supplied?
// TODO: return default or do we want to measure explicit configuration?
configuredRtcpMuxPolicy: function(client, peerConnectionLog) {
var peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
return peerConnectionConfig ? peerConnectionConfig.rtcpMuxPolicy !== undefined : false; // default: 'require'
},
// what iceTransportPolicy configuration was supplied?
// TODO: return default or do we want to measure explicit configuration?
configuredIceTransportPolicy: function(client, peerConnectionLog) {
var peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
return peerConnectionConfig ? peerConnectionConfig.iceTransportPolicy !== undefined : false; // default: 'all'
},
// was the peerconnection created with a RTCCertificate
configuredCertificate: function(client, peerConnectionLog) {
var peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
return peerConnectionConfig ? peerConnectionConfig.certificates !== undefined : false;
},
// did ice gathering complete (aka: onicecandidate called with a null candidate)
ICEGatheringComplete: function(client, peerConnectionLog) {
return peerConnectionLog.filter(function(entry) {
return entry.type === 'onicecandidate' && entry.value === null;
}).length > 0;
},
// was an ice failure detected.
ICEFailure: function(client, peerConnectionLog) {
var i = 0;
var iceRestart = false;
for (; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'oniceconnectionstatechange' && peerConnectionLog[i].value === 'failed') {
return true;
}
}
return false;
},
// was an ice failure after a successful connection detected.
ICEFailureSubsequent: function(client, peerConnectionLog) {
var i = 0;
var connected = false;
for (; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'oniceconnectionstatechange' && (peerConnectionLog[i].value === 'connected' || peerConnectionLog[i].value === 'completed')) {
connected = true;
break;
}
}
if (connected) {
for (; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'oniceconnectionstatechange' && peerConnectionLog[i].value === 'failed') {
return true;
}
}
}
return false;
},
// did ice connect/complete?
ICEConnectedOrCompleted: function(client, peerConnectionLog) {
return peerConnectionLog.filter(function(entry) {
return entry.type === 'oniceconnectionstatechange' && (entry.value === 'connected' || entry.value === 'completed');
}).length > 0;
},
// Firefox has a timeout of ~5 seconds where addIceCandidate needs to happen after SRD.
// This calculates the delay between SRD and addIceCandidate which should allow
// correlation with ICE failures caused by this.
// returns -1 if addIceCandidate is called before setRemoteDescription
timeBetweenSetRemoteDescriptionAndAddIceCandidate: function(client, peerConnectionLog) {
var srd;
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'setRemoteDescription') {
srd = peerConnectionLog[i].time;
} else if (peerConnectionLog[i].type === 'addIceCandidate') {
if (srd) {
return new Date(peerConnectionLog[i].time).getTime() - new Date(srd).getTime();
} else {
return -1;
}
}
}
},
// This calculates the delay between SLD and onicecandidate.
timeBetweenSetLocalDescriptionAndOnIceCandidate: function(client, peerConnectionLog) {
var sld;
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'setLocalDescription') {
sld = peerConnectionLog[i].time;
} else if (peerConnectionLog[i].type === 'onicecandidate') {
if (sld) {
return new Date(peerConnectionLog[i].time).getTime() - new Date(sld).getTime();
} else {
return -1; // should not happen but...
}
}
}
},
// detects the osx audio bug which manifests as "audio seems to work but
// no audio is ever sent (which manifests as bytesSent always being 0)
// https://bugs.chromium.org/p/webrtc/issues/detail?id=4799
notsendingaudio: function(client, peerConnectionLog) {
var track = extractTrack(peerConnectionLog, 'audio', 'send');
if (!(track && track.length)) return false;
var count = 0;
for (var i = 0; i < track.length; i++) {
if (parseInt(track[i].bytesSent, 10) > 0) return false;
count++;
}
return count > 0;
},
// detect cam being used by another application (no bytes sent for video)
// https://bugs.chromium.org/p/chromium/issues/detail?id=403710#c7
notsendingvideo: function(client, peerConnectionLog) {
var track = extractTrack(peerConnectionLog, 'video', 'send');
if (!(track && track.length)) return false;
var count = 0;
for (var i = 0; i < track.length; i++) {
if (parseInt(track[i].bytesSent, 10) > 0) return false;
count++;
}
return count > 0;
},
// check whether video is received after 10 seconds
receivingvideo10s: function(client, peerConnectionLog) {
var count = 0;
var receivedVideo = undefined;
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'getStats') {
if (count++ === 10) {
Object.keys(peerConnectionLog[i].value).forEach(function(id) {
var report = peerConnectionLog[i].value[id];
if (report.type === 'ssrc' && report.mediaType === 'video' && id.indexOf('_recv')) {
receivedVideo = {
packetsReceived: report.packetsReceived,
frameWidth: report.frameWidth,
frameHeight: report.frameHeight
};
}
});
return receivedVideo;
}
}
}
},
// how long did it take until video arrived?
// in particular a keyframe which causes width and height to be set.
timeuntilreceivingvideo: function(client, peerConnectionLog) {
let timeConnected = false;
let timeReceived = false;
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'oniceconnectionstatechange' && !timeConnected) {
if (peerConnectionLog[i].value === 'connected' || peerConnectionLog[i].value === 'completed') {
timeConnected = new Date(peerConnectionLog[i].time).getTime();
}
}
if (peerConnectionLog[i].type === 'getStats') {
Object.keys(peerConnectionLog[i].value).forEach(function(id) {
var report = peerConnectionLog[i].value[id];
if (report.type === 'ssrc' && report.mediaType === 'video' && report.googFrameWidthReceived) {
let width = parseInt(report.googFrameWidthReceived, 10);
if (width > 0) {
timeReceived = new Date(peerConnectionLog[i].time).getTime();
}
}
});
}
if (timeReceived && timeConnected) {
return timeReceived - timeConnected;
}
}
},
// check whether audio is received after 10 seconds
receivingaudio10s: function(client, peerConnectionLog) {
var count = 0;
var receivedAudio = undefined;
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'getStats') {
if (count++ === 10) {
Object.keys(peerConnectionLog[i].value).forEach(function(id) {
var report = peerConnectionLog[i].value[id];
if (report.type === 'ssrc' && report.mediaType === 'audio' && id.indexOf('_recv')) {
receivedAudio = {
packetsReceived: report.packetsReceived,
jitterbufferms: report.googJitterBufferMs
};
}
});
return receivedAudio;
}
}
}
},
// is the session using ICE lite?
usingICELite: function(client, peerConnectionLog) {
var usingIceLite = false;
peerConnectionLog.forEach(function(entry) {
if (!usingIceLite && entry.type === 'setRemoteDescription') {
if (entry.value.sdp && entry.value.sdp.indexOf('\r\na=ice-lite\r\n') !== -1) {
usingIceLite = true;
}
}
});
return usingIceLite;
},
// is the session using rtcp-mux?
usingRTCPMux: function(client, peerConnectionLog) {
var usingRTCPMux = false;
// search for SLD/SRD with type = answer and look for a=rtcp-mux
peerConnectionLog.forEach(function(entry) {
if (!usingRTCPMux && (entry.type === 'setRemoteDescription' || entry.type === 'setLocalDescription')) {
if (entry.value.type === 'answer' && entry.value.sdp && entry.value.sdp.indexOf('\r\na=rtcp-mux\r\n') !== -1) {
usingRTCPMux = true;
}
}
});
return usingRTCPMux;
},
// is the session using BUNDLE?
usingBundle: function(client, peerConnectionLog) {
var usingBundle = false;
// search for SLD/SRD with type = answer and look for a=GROUP
peerConnectionLog.forEach(function(entry) {
if (!usingBundle && (entry.type === 'setRemoteDescription' || entry.type === 'setLocalDescription')) {
if (entry.value.type === 'answer' && entry.value.sdp && entry.value.sdp.indexOf('\r\na=group:BUNDLE ') !== -1) {
usingBundle = true;
}
}
});
return usingBundle;
},
ICERestart: function(client, peerConnectionLog) {
var iceRestart = false;
peerConnectionLog.forEach(function(entry) {
if (!iceRestart && entry.type === 'createOffer') {
if (entry.value && entry.value.iceRestart) {
iceRestart = true;
}
}
});
return iceRestart;
},
ICERestartSuccess: function(client, peerConnectionLog) {
var i = 0;
var iceRestart = false;
for (; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'createOffer' && peerConnectionLog[i].value && peerConnectionLog[i].value.iceRestart) {
iceRestart = true;
break;
}
}
if (iceRestart) {
for (; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'oniceconnectionstatechange' &&
(peerConnectionLog[i].value === 'connected' || peerConnectionLog[i].value === 'completed')) return true;
}
}
return false;
},
// was setRemoteDescription called after the ice restart? If not the peer
// went away.
ICERestartFollowedBySetRemoteDescription: function(client, peerConnectionLog) {
var i = 0;
var iceRestart = false;
for (; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'createOffer' && peerConnectionLog[i].value && peerConnectionLog[i].value.iceRestart) {
iceRestart = true;
break;
}
}
if (iceRestart) {
for (; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'setRemoteDescription') return true;
}
return false;
}
},
// was there a relay candidate gathered after the ice restart?
ICERestartFollowedByRelayCandidate: function(client, peerConnectionLog) {
var i = 0;
var iceRestart = false;
for (; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'createOffer' && peerConnectionLog[i].value && peerConnectionLog[i].value.iceRestart) {
iceRestart = true;
break;
}
}
if (iceRestart) {
for (; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'onicecandidate') {
var cand = peerConnectionLog[i].value;
if (cand === null) return false; // give up
if (cand && cand.candidate.indexOf('relay') !== -1) {
return true;
}
}
}
return false;
}
},
// was the signaling state stable at least once?
signalingStableAtLeastOnce: function(client, peerConnectionLog) {
return peerConnectionLog.filter(function(entry) {
return entry.type === 'onsignalingstatechange' && entry.value === 'stable';
}).length > 0;
},
// was more than one remote stream added?
usingMultistream: function(client, peerConnectionLog) {
return peerConnectionLog.filter(function(entry) {
return entry.type === 'onaddstream';
}).length > 1;
},
// maximum number of concurrent streams
maxStreams: function(client, peerConnectionLog) {
var max = 0;
peerConnectionLog.forEach(function(entry) {
if (entry.type === 'onaddstream') max++;
else if (entry.type === 'onremovestream' && max > 0) max--;
});
return max;
},
firstRemoteStream: function(client, peerConnectionLog) {
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'onaddstream') {
let value = peerConnectionLog[i].value;
let feature = {
id: value.split(' ')[0]
};
value.split(' ')[1].split(',').forEach((kindAndTrack) => {
let parts = kindAndTrack.split(':', 2);
feature[parts[0]] = parts[1];
});
return feature;
}
}
},
usingSimulcast: function(client, peerConnectionLog) {
for (var i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'setLocalDescription') {
var value = peerConnectionLog[i].value;
return value && value.sdp && value.sdp.indexOf('a=ssrc-group:SIM') !== -1;
}
}
},
// was there a setLocalDescription failure?