forked from vpalmisano/webrtcperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
observertc.js
72 lines (63 loc) · 2.4 KB
/
observertc.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
console.log('Creating ObserverRTC');
window.observer = new ObserverRTC
.Builder({wsAddress: '', poolingIntervalInMs: 1000 * window.STATS_INTERVAL})
.withIntegration('General')
.withLocalTransport({
onObserverRTCSample: (sampleList) => {
window.traceRtcStats(sampleList);
},
})
.build();
console.log('Override RTCPeerConnection');
const NativeRTCPeerConnection = window.RTCPeerConnection;
window.RTCPeerConnection = function(...args) {
const pc = new NativeRTCPeerConnection(...args);
console.log(`RTCPeerConnection add (state: ${pc.signalingState})`);
window.observer.addPC(pc);
const interval = setInterval(async () => {
/* const stats = await pc.getStats();
for (const s of stats) {
console.log('RTCPeerConnection ', JSON.stringify(s, null, 2));
} */
if (pc.signalingState === 'closed' || pc.signalingState === 'failed') {
console.warn(`RTCPeerConnection remove (state: ${pc.signalingState})`);
window.observer.removePC(pc);
window.clearInterval(interval);
}
}, 2000);
return pc;
};
for (const key of Object.keys(NativeRTCPeerConnection)) {
window.RTCPeerConnection[key] = NativeRTCPeerConnection[key];
}
window.RTCPeerConnection.prototype = NativeRTCPeerConnection.prototype;
console.log('Override getUserMedia');
/**
* overrideGetUserMedia
* @param {*} constraints
*/
function overrideGetUserMedia(constraints) {
if (window.GET_USER_MEDIA_OVERRIDE) {
if (constraints.video && window.GET_USER_MEDIA_OVERRIDE.video) {
Object.assign(constraints.video, window.GET_USER_MEDIA_OVERRIDE.video);
}
if (constraints.audio && window.GET_USER_MEDIA_OVERRIDE.audio) {
Object.assign(constraints.audio, window.GET_USER_MEDIA_OVERRIDE.audio);
}
console.log('getUserMedia override result:',
JSON.stringify(constraints, null, 2));
}
}
const nativeGetUserMedia = navigator.getUserMedia;
navigator.getUserMedia = function(constraints) {
overrideGetUserMedia(constraints, ...args);
return nativeGetUserMedia.apply(navigator, [constraints, ...args]);
};
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
const nativeGetUserMedia = navigator.mediaDevices.getUserMedia;
navigator.mediaDevices.getUserMedia = function(constraints, ...args) {
overrideGetUserMedia(constraints);
return nativeGetUserMedia.apply(navigator.mediaDevices,
[constraints, ...args]);
};
}