-
Notifications
You must be signed in to change notification settings - Fork 44
/
features-connection.js
1294 lines (1205 loc) · 53.4 KB
/
features-connection.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.
// there are three 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.
// 3) features which are specific to a track.
// The second type of feature is contained in this file.
const {capitalize, standardizedMoment, timeBetween, isIceConnected} = require('./utils');
const SDPUtils = require('sdp');
function getPeerConnectionConfig(peerConnectionLog) {
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'create') {
return peerConnectionLog[i].value || {nullConfig: true};
}
}
}
function getPeerConnectionConstraints(peerConnectionLog) {
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'constraints') {
return peerConnectionLog[i].value;
}
}
return {};
}
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) {
const peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
if (!peerConnectionConfig) {
return;
}
let 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;
}
let first;
let 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') {
const cand = peerConnectionLog[second].value;
if (cand === null) return; // give up
if (cand && cand.candidate.indexOf('relay') !== -1) {
const localTypePref = cand.candidate.split(' ')[3] >> 24;
if (localTypePref === typepref) {
break;
}
}
}
}
if (second < peerConnectionLog.length) {
return peerConnectionLog[second].timestamp - peerConnectionLog[first].timestamp;
}
}
}
function extractBWE(peerConnectionLog) {
const reports = [];
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'getStats') {
const statsReport = peerConnectionLog[i].value;
Object.keys(statsReport).forEach(id => {
const report = statsReport[id];
if (report.type === 'VideoBwe') {
reports.push(report);
}
});
}
}
return reports;
}
module.exports = {
// 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) {
let constraints = getPeerConnectionConstraints(peerConnectionLog);
if (!constraints.optional) return;
constraints = constraints.optional;
for (let i = 0; i < constraints.length; i++) {
if (constraints[i].rtcStatsClientId) {
return constraints[i].rtcStatsClientId;
}
}
if (client.identity) {
return client.identity.user;
}
},
peerIdentifier: function(client, peerConnectionLog) {
let constraints = getPeerConnectionConstraints(peerConnectionLog);
if (!constraints.optional) return;
constraints = constraints.optional;
for (let i = 0; i < constraints.length; i++) {
if (constraints[i].rtcStatsPeerId) {
return constraints[i].rtcStatsPeerId;
}
}
},
conferenceIdentifier: function(client, peerConnectionLog) {
let constraints = getPeerConnectionConstraints(peerConnectionLog);
if (!constraints.optional) return;
constraints = constraints.optional;
for (let i = 0; i < constraints.length; i++) {
if (constraints[i].rtcStatsConferenceId) {
return constraints[i].rtcStatsConferenceId;
}
}
if (client.identity) {
return client.identity.conference;
}
},
sfuP2P: function(client, peerConnectionLog) {
let constraints = getPeerConnectionConstraints(peerConnectionLog) || [];
if (!constraints.optional) return;
constraints = constraints.optional;
for (let i = 0; i < constraints.length; i++) {
if (constraints[i].rtcStatsSFUP2P) {
return constraints[i].rtcStatsSFUP2P;
}
}
},
// when did the session start
startTime: function(client, peerConnectionLog) {
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'create') {
return peerConnectionLog[i].timestamp;
}
}
},
// when did the session end
stopTime: function(client, peerConnectionLog) {
return peerConnectionLog[peerConnectionLog.length - 1].timestamp;
},
// how long did the peerconnection live?
// not necessarily connected which is different from session duration
lifeTime: function(client, peerConnectionLog) {
const lifeTime = peerConnectionLog[peerConnectionLog.length - 1].timestamp - peerConnectionLog[0].timestamp;
return lifeTime > 0 ? lifeTime : undefined;
},
sendingDuration: function(client, peerConnectionLog) {
let sendingDuration = 0;
let prevTime = peerConnectionLog[0].timestamp;
let prevSending = false;
peerConnectionLog.forEach(({type, value, timestamp}) => {
if (type !== 'setLocalDescription') {
return;
}
const sections = SDPUtils.getMediaSections(value.sdp);
if (!sections.length) {
return;
}
const direction = SDPUtils.getDirection(sections[0]);
const logSending = ['sendonly', 'sendrecv'].includes(direction);
if (prevSending) {
sendingDuration += timestamp - prevTime;
}
prevTime = timestamp;
prevSending = logSending;
});
if (prevSending) {
sendingDuration += peerConnectionLog[peerConnectionLog.length - 1].timestamp - prevTime;
}
return sendingDuration;
},
// the webrtc platform type -- webkit or moz
// TODO: edge, mobile platforms?
browserType: function(client, peerConnectionLog) {
const peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
return (peerConnectionConfig && 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 (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'setRemoteDescription') {
const 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 (let 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) {
const 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) {
const peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
return !!(peerConnectionConfig && peerConnectionConfig.iceServers !== undefined)
},
// was STUN configured in the peerconnection config?
configuredWithSTUN: function(client, peerConnectionLog) {
const peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
if (!(peerConnectionConfig && peerConnectionConfig.iceServers)) return;
for (let i = 0; i < peerConnectionConfig.iceServers.length; i++) {
let urls = peerConnectionConfig.iceServers[i].urls || [];
if (typeof urls === 'string') {
urls = [urls];
}
for (let 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) {
const peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
if (!(peerConnectionConfig && peerConnectionConfig.iceServers)) return;
for (let i = 0; i < peerConnectionConfig.iceServers.length; i++) {
const urls = peerConnectionConfig.iceServers[i].urls || [];
for (let 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) {
const peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
if (!(peerConnectionConfig && peerConnectionConfig.iceServers)) return;
for (let i = 0; i < peerConnectionConfig.iceServers.length; i++) {
const urls = peerConnectionConfig.iceServers[i].urls || [];
for (let 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) {
const peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
if (!(peerConnectionConfig && peerConnectionConfig.iceServers)) return;
for (let i = 0; i < peerConnectionConfig.iceServers.length; i++) {
const urls = peerConnectionConfig.iceServers[i].urls || [];
for (let 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) {
const peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
if (!(peerConnectionConfig && peerConnectionConfig.iceServers)) return;
for (let i = 0; i < peerConnectionConfig.iceServers.length; i++) {
const urls = peerConnectionConfig.iceServers[i].urls || [];
for (let 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) {
const 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) {
const 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) {
const peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
return peerConnectionConfig ? peerConnectionConfig.iceTransportPolicy !== undefined : false; // default: 'all'
},
// was the peerconnection created with a RTCCertificate
configuredCertificate: function(client, peerConnectionLog) {
const peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
return peerConnectionConfig ? peerConnectionConfig.certificates !== undefined : false;
},
// was the peerconnection created with non-spec SDES?
configuredSDES: function(client, peerConnectionLog) {
const constraints = getPeerConnectionConstraints(peerConnectionLog);
return constraints && constraints.mandatory && constraints.mandatory.DtlsSrtpKeyAgreement === false;
},
sdpSemantics: function(client, peerConnectionLog) {
const peerConnectionConfig = getPeerConnectionConfig(peerConnectionLog);
return peerConnectionConfig ? peerConnectionConfig.sdpSemantics : '';
},
// did ice gathering complete (aka: onicecandidate called with a null candidate)
ICEGatheringComplete: function(client, peerConnectionLog) {
return peerConnectionLog.filter(entry => entry.type === 'onicecandidate' && entry.value === null).length > 0;
},
// was an ice failure detected.
ICEFailure: function(client, peerConnectionLog) {
for (let i = 0; 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) {
let i = 0;
let connected = false;
for (; i < peerConnectionLog.length; i++) {
if (isIceConnected(peerConnectionLog[i])) {
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(entry => isIceConnected(entry)).length > 0;
},
// ICE connected but connectionState not indicates a DTLS failure
dtlsFailure: function(client, peerConnectionLog) {
let iceConnected = false;
let connected = false;
for (let i = 0; i < peerConnectionLog.length; i++) {
if (isIceConnected(peerConnectionLog[i])) {
iceConnected = true;
}
if (peerConnectionLog[i].type === 'onconnectionstatechange' && peerConnectionLog[i].value === 'connected') {
connected = true;
}
}
if (iceConnected && !connected) {
return true;
} else if (iceConnected && connected) {
return false;
}
},
iceconnectionstateCheckingBeforeSRD: function(client, peerConnectionLog) {
// https://bugs.chromium.org/p/chromium/issues/detail?id=959128#c65
// Sometimes, iceconnectionstatechange can fire before
// SRD/addIceCandidate. This happens when we are offering and
// the remote does a valid stun ping to the port before the answer
// arrives.
let hadSRD = false;
for (let i = 0; i < peerConnectionLog.length; i++) {
const {type, value} = peerConnectionLog[i];
if (type === 'setRemoteDescription') {
hadSRD = true;
} else if (type === 'oniceconnectionstatechange' && value === 'checking') {
return !hadSRD;
}
}
},
// 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) {
return timeBetween(peerConnectionLog, ['setRemoteDescription'], ['addIceCandidate']);
},
// This calculates the delay between SLD and onicecandidate.
timeBetweenSetLocalDescriptionAndOnIceCandidate: function(client, peerConnectionLog) {
return timeBetween(peerConnectionLog, ['setLocalDescription'], ['onicecandidate']);
},
// This calculates the time between the first SRD and resolving.
timeForFirstSetRemoteDescription: function(client, peerConnectionLog) {
return timeBetween(peerConnectionLog, ['setRemoteDescription'], ['setRemoteDescriptionOnSuccess']);
},
// determines whether the first setRemoteDescription resulted in an ontrack event.
ontrackAfterFirstSetRemoteDescription: function(client, peerConnectionLog) {
let i;
for (i = 0; i < peerConnectionLog.length; i++) { // search for setRemoteDescription.
if (peerConnectionLog[i].type === 'setRemoteDescription') {
break;
}
}
for(; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'ontrack') {
return true;
}
if (peerConnectionLog[i].type === 'setRemoteDescriptionOnSuccess') {
return false;
}
}
},
// This calculates the time between the second SRD and resolving.
timeForSecondSetRemoteDescription: function(client, peerConnectionLog) {
let i;
for (i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'setRemoteDescriptionOnSuccess') {
return timeBetween(peerConnectionLog.slice(i + 1), ['setRemoteDescription'], ['setRemoteDescriptionOnSuccess']);
}
}
},
// is the session using ICE lite?
usingICELite: function(client, peerConnectionLog) {
let usingIceLite = false;
peerConnectionLog.forEach(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) {
let usingRTCPMux = false;
// search for SLD/SRD with type = answer and look for a=rtcp-mux
peerConnectionLog.forEach(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) {
let usingBundle = false;
// search for SLD/SRD with type = answer and look for a=GROUP
peerConnectionLog.forEach(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) {
let iceRestart = false;
peerConnectionLog.forEach(entry => {
if (!iceRestart && entry.type === 'createOffer') {
if (entry.value && entry.value.iceRestart) {
iceRestart = true;
}
}
});
return iceRestart;
},
ICERestartSuccess: function(client, peerConnectionLog) {
let i = 0;
let 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 (isIceConnected(peerConnectionLog[i])) {
return true;
}
}
}
return false;
},
// was setRemoteDescription called after the ice restart? If not the peer
// went away.
ICERestartFollowedBySetRemoteDescription: function(client, peerConnectionLog) {
let i = 0;
let 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) {
let i = 0;
let 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') {
const 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(entry => entry.type === 'onsignalingstatechange' && entry.value === 'stable').length > 0;
},
// was more than one remote stream added?
usingMultistream: function(client, peerConnectionLog) {
return peerConnectionLog.filter(entry => entry.type === 'onaddstream').length > 1;
},
// maximum number of concurrent streams
maxStreams: function(client, peerConnectionLog) {
let max = 0;
peerConnectionLog.forEach(entry => {
if (entry.type === 'onaddstream') max++;
else if (entry.type === 'onremovestream' && max > 0) max--;
});
return max;
},
numberOfRemoteStreams: function(client, peerConnectionLog) {
const remoteStreams = {};
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'ontrack') {
const {value} = peerConnectionLog[i];
const streamId = value.split(' ')[1];
remoteStreams[streamId] = true;
}
}
return Object.keys(remoteStreams).length;
},
usingSimulcast: function(client, peerConnectionLog) {
for (let i = 0; i < peerConnectionLog.length; i++) {
const {type, value} = peerConnectionLog[i];
if (type === 'setLocalDescription') {
const simulcast = value && value.sdp && (value.sdp.indexOf('a=ssrc-group:SIM ') !== -1 || value.sdp.indexOf('a=simulcast:') !== -1);
if (simulcast) {
return true;
}
}
}
return false;
},
numberOfLocalSimulcastStreams: function(client, peerConnectionLog) {
for (let i = 0; i < peerConnectionLog.length; i++) {
const {type, value} = peerConnectionLog[i];
if (type === 'setLocalDescription') {
const simulcast = value && value.sdp && (value.sdp.indexOf('a=ssrc-group:SIM ') !== -1); // Chrome-only definition.
if (simulcast) {
const line = SDPUtils.splitLines(value.sdp)
.filter(line => line.indexOf('a=ssrc-group:SIM ') === 0);
return line[0].substr(17).split(' ').length;
}
}
}
},
// was there a setLocalDescription failure?
setLocalDescriptionFailure: function(client, peerConnectionLog) {
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'setLocalDescriptionOnFailure') {
return peerConnectionLog[i].value;
}
}
},
// was there a setRemoteDescription failure?
setRemoteDescriptionFailure: function(client, peerConnectionLog) {
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'setRemoteDescriptionOnFailure') {
return peerConnectionLog[i].value;
}
}
},
// was there an addIceCandidate failure
addIceCandidateFailure: function(client, peerConnectionLog) {
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'addIceCandidateOnFailure') {
return peerConnectionLog[i].value;
}
}
},
// how long did it take to gather all ice candidates?
gatheringTime: function(client, peerConnectionLog) {
let first;
let 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' && peerConnectionLog[second].value === null) break;
}
if (second < peerConnectionLog.length) {
return peerConnectionLog[second].timestamp - peerConnectionLog[first].timestamp;
}
}
},
// was a local host candidate gathered. This should always be true.
// And yet I saw a pig flying with Firefox 46 on Windows which did
// not like a teredo interface and did not gather candidates.
gatheredHost: function(client, peerConnectionLog) {
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'onicecandidate') {
const cand = peerConnectionLog[i].value;
if (cand === null) return false; // gathering finished so we have seen all candidates.
if (cand.candidate.indexOf('host') !== -1) {
return true;
}
}
}
},
// was a local STUN candidate gathered?
// TODO: do we care about timing?
gatheredSTUN: function(client, peerConnectionLog) {
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'onicecandidate') {
const cand = peerConnectionLog[i].value;
if (cand === null) return false; // gathering finished so we have seen all candidates.
if (cand.candidate.indexOf('srflx') !== -1) {
return true;
}
}
}
return false;
},
// was a local TURN/UDP relay candidate gathered?
gatheredTURNUDP: function(client, peerConnectionLog) {
return gatheringTimeTURN('udp', client, peerConnectionLog) !== undefined;
},
// how long did it take to gather a TURN/UDP relay candidate
gatheringTimeTURNUDP: function(client, peerConnectionLog) {
return gatheringTimeTURN('udp', client, peerConnectionLog);
},
// was a local TURN/TCP relay candidate gathered?
gatheredTURNTCP: function(client, peerConnectionLog) {
return gatheringTimeTURN('tcp', client, peerConnectionLog) !== undefined;
},
// how long did it take to gather a TURN/TCP relay candidate
gatheringTimeTURNTCP: function(client, peerConnectionLog) {
return gatheringTimeTURN('tcp', client, peerConnectionLog);
},
// was a local TURN/TLS relay candidate gathered?
gatheredTURNTLS: function(client, peerConnectionLog) {
return gatheringTimeTURN('tls', client, peerConnectionLog) !== undefined;
},
// how long did it take to gather a TURN/TLS relay candidate
gatheringTimeTURNTLS: function(client, peerConnectionLog) {
return gatheringTimeTURN('tls', client, peerConnectionLog);
},
// which turn server was used? returns the relay address.
relayAddress: function(client, peerConnectionLog) {
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'onicecandidate') {
const cand = peerConnectionLog[i].value;
if (cand === null) return; // give up
if (cand && cand.candidate.indexOf('relay') !== -1) {
return cand.candidate.split(' ')[4];
}
}
}
},
// was there a remote candidate TURN added?
// that is about as much as we can tell unless we snoop onto the
// peerconnection and determine remote browser.
hadRemoteTURNCandidate: function(client, peerConnectionLog) {
// TODO: might be hiding in setRemoteDescription, too.
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'addIceCandidate') {
const cand = peerConnectionLog[i].value;
if (cand && cand.candidate && cand.candidate.indexOf('relay') !== -1) {
return true;
}
}
}
return false;
},
// what types of RFC 1918 private ip addresses were gathered?
gatheredrfc1918address: function(client, peerConnectionLog) {
const gathered = {};
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'onicecandidate') {
const cand = peerConnectionLog[i].value;
if (cand === null) break; // gathering done
if (cand.candidate) {
const ip = cand.candidate.split(' ')[4];
if (ip.indexOf('192.168.') === 0) gathered.prefix16 = true;
else if (ip.indexOf('172.') === 0) {
const secondByte = ip.split('.')[1] >>> 0;
if (secondByte >= 16 && secondByte <= 31) {
gathered.prefix12 = true;
}
} else if (ip.indexOf('10.') === 0) gathered.prefix10 = true;
}
}
}
if (Object.keys(gathered).length) {
return gathered;
}
},
// estimates the number of interfaces
numberOfInterfaces: function(client, peerConnectionLog) {
const ips = {};
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'onicecandidate') {
const cand = peerConnectionLog[i].value;
if (cand === null) break; // gathering finished so we have seen all candidates.
const parts = cand.candidate.split(' ');
if (parts[7] === 'host') {
if (!ips[parts[4]]) ips[parts[4]] = 0;
ips[parts[4]]++;
}
}
}
return Object.keys(ips).length;
},
// how long does it take to establish the connection?
connectionTime: function(client, peerConnectionLog) {
let first;
let second;
for (first = 0; first < peerConnectionLog.length; first++) {
if (peerConnectionLog[first].type === 'onconnectionstatechange' &&
peerConnectionLog[first].value === 'connecting') break;
}
if (first < peerConnectionLog.length) {
for (second = first + 1; second < peerConnectionLog.length; second++) {
if (peerConnectionLog[second].type === 'onconnectionstatechange' &&
peerConnectionLog[second].value === 'connected') break;
}
if (second < peerConnectionLog.length) {
return peerConnectionLog[second].timestamp - peerConnectionLog[first].timestamp;
}
}
},
// how long does it take to establish the ice connection?
iceConnectionTime: function(client, peerConnectionLog) {
let first;
let second;
for (first = 0; first < peerConnectionLog.length; first++) {
if (peerConnectionLog[first].type === 'oniceconnectionstatechange' &&
peerConnectionLog[first].value === 'checking') break;
}
if (first < peerConnectionLog.length) {
for (second = first + 1; second < peerConnectionLog.length; second++) {
if (isIceConnected(peerConnectionLog[second])) {
break;
}
}
if (second < peerConnectionLog.length) {
return peerConnectionLog[second].timestamp - peerConnectionLog[first].timestamp;
}
}
},
// how long does it take to create a local offer/answer (mostly DTLS key generation)
localCreateDelay: function(client, peerConnectionLog) {
let first;
let second;
for (first = 0; first < peerConnectionLog.length; first++) {
if (peerConnectionLog[first].type === 'createOffer' ||
peerConnectionLog[first].type === 'createAnswer') break;
}
if (first < peerConnectionLog.length) {
for (second = first + 1; second < peerConnectionLog.length; second++) {
if (peerConnectionLog[second].type === peerConnectionLog[first].type + 'OnSuccess') break;
}
if (second < peerConnectionLog.length) {
return peerConnectionLog[second].timestamp - peerConnectionLog[first].timestamp;
}
}
return -1;
},
// number of local ice candidates.
numberOfLocalIceCandidates: function(client, peerConnectionLog) {
return peerConnectionLog.filter(entry => entry.type === 'onicecandidate' && entry.value).length;
},
// number of remote ice candidates.
numberOfRemoteIceCandidates: function(client, peerConnectionLog) {
let candsInSdp = -1;
// needs sentinel to avoid adding candidates from subsequent generations.
peerConnectionLog.forEach(entry => {
if (candsInSdp === -1 && entry.type === 'setRemoteDescription') {
if (entry.value.sdp) {
candsInSdp = entry.value.sdp.split('\n').filter(line => line.indexOf('a=candidate:') === 0).length;
}
}
});
if (candsInSdp === -1) candsInSdp = 0;
return candsInSdp + peerConnectionLog.filter(entry => entry.type === 'addIceCandidate').length;
},
// session duration, defined by ICE states.
sessionDuration: function(client, peerConnectionLog) {
let startTime = -1;
let endTime = -1;
let i;
for (i = 0; i < peerConnectionLog.length; i++) {
if (isIceConnected(peerConnectionLog[i]) && startTime === -1) {
startTime = peerConnectionLog[i].timestamp;
break;
}
}
if (startTime > 0) {
// TODO: this is too simplistic. What if the ice connection state went to failed?
for (let j = peerConnectionLog.length - 1; j > i; j--) {
endTime = peerConnectionLog[j].timestamp;
if (startTime < endTime && endTime > 0) {
return endTime - startTime;
}
}
}
},
// determine media types used in session.
mediaTypes: function(client, peerConnectionLog) {
// looking for SRD/SLD is easier than tracking createDataChannel + addStreams
// TODO: also look for value.type=answer and handle rejected m-lines?
let i;
for (i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type === 'setLocalDescription' ||
peerConnectionLog[i].type === 'setRemoteDescription') break;
}
if (i < peerConnectionLog.length) {
const desc = peerConnectionLog[i].value;
if (desc && desc.sdp) {
const mediaTypes = {};
const lines = desc.sdp.split('\n').filter(line => line.indexOf('m=') === 0);
lines.forEach(line => {
mediaTypes[line.split(' ', 1)[0].substr(2)] = true;
});
return Object.keys(mediaTypes).sort().join(';');
}
}
return 'unknown';
},
// dlts cipher suite used
// TODO: what is the standard thing for that?
dtlsCipherSuite: function(client, peerConnectionLog) {
let dtlsCipher;
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type !== 'getStats') continue;
const statsReport = peerConnectionLog[i].value;
Object.keys(statsReport).forEach(id => {
const report = statsReport[id];
if (report.type === 'googComponent' && report.dtlsCipher) {
dtlsCipher = report.dtlsCipher;
}
});
if (dtlsCipher) return dtlsCipher;
}
},
// srtp cipher suite used
srtpCipherSuite: function(client, peerConnectionLog) {
let srtpCipher;
for (let i = 0; i < peerConnectionLog.length; i++) {
if (peerConnectionLog[i].type !== 'getStats') continue;
const statsReport = peerConnectionLog[i].value;
Object.keys(statsReport).forEach(id => {
const report = statsReport[id];
if (report.type === 'googComponent' && report.srtpCipher) {
srtpCipher = report.srtpCipher;
}
});
if (srtpCipher) return srtpCipher;
}
},
// mean RTT, send and recv bitrate of the active candidate pair
statsMean: function(client, peerConnectionLog) {
const feature = {};
const rtts = [];
const recv = [];
const send = [];
let lastStatsReport;
let lastTime;
peerConnectionLog.forEach(entry => {
if (entry.type !== 'getStats') return;
const statsReport = entry.value;
// look for type track, remoteSource: false, audioLevel (0..1)
Object.keys(statsReport).forEach(id => {
const report = statsReport[id];
if (report.type === 'candidate-pair' && report.selected === true) {
rtts.push(report.roundTripTime);
}
});
if (lastStatsReport) {