-
Notifications
You must be signed in to change notification settings - Fork 19
/
encrypted.html
140 lines (132 loc) · 4.18 KB
/
encrypted.html
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
<html>
<head>
<meta charset="utf-8">
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script src="https://webrtc.github.io/samples/src/js/third_party/graph.js"></script>
<script src="split-merge.js"></script>
<script src="test-assert.js"></script>
<script src="draw-graphs.js"></script>
<style>
video {
width: 320px;
}
</style>
</head>
<body>
<div id="local">
<h2>Local Video</h2>
</div>
<div id="remotes">
<h2>Remote Videos</h2>
</div>
<script>
const SDPUtils = adapter.sdp;
const searchParameters = new URLSearchParams(window.location.search);
const scaleDown = {0: 4, 1: 2, 2: 1};
const rids = [0, 1, 2];
const streamIds = {0: 'low', 1: 'mid', 2: 'hi'};
const video = {
'1080p': {width: 1920, height: 1080},
'720p': {width: 1280, height: 720},
'360p': {width: 640, height: 360},
}[searchParameters.get('resolution') || '720p'];
const scalabilityMode = searchParameters.get('scalabilityMode') || undefined;
const remb = searchParameters.has('remb');
let currentCryptoKey;
function sendTransform(chunk, controller) {
console.log('ST', chunk.data.byteLength);
if (currentCryptoKey) {
const view = new DataView(chunk.data);
// Any length that is needed can be used for the new buffer.
const newData = new ArrayBuffer(chunk.data.byteLength + 4);
const newView = new DataView(newData);
for (let i = 0; i < chunk.data.byteLength; ++i) {
const keyByte = currentCryptoKey.charCodeAt(i % currentCryptoKey.length);
newView.setInt8(i, view.getInt8(i) ^ keyByte);
}
// Append checksum
newView.setUint32(chunk.data.byteLength, 0xDEADBEEF);
chunk.data = newData;
}
controller.enqueue(chunk);
}
function receiveTransform(chunk, controller) {
console.log('RT', chunk.data.byteLength);
if (currentCryptoKey) {
const view = new DataView(chunk.data);
const checksum = view.getUint32(chunk.data.byteLength - 4);
if (checksum != 0xDEADBEEF) {
console.log('Corrupted frame received');
console.log(checksum.toString(16));
}
const newData = new ArrayBuffer(chunk.data.byteLength - 4);
const newView = new DataView(newData);
for (let i = 0; i < chunk.data.byteLength - 4; ++i) {
const keyByte = currentCryptoKey.charCodeAt(i % currentCryptoKey.length);
newView.setInt8(i, view.getInt8(i) ^ keyByte);
}
chunk.data = newData;
}
controller.enqueue(chunk);
}
const pc1 = new RTCPeerConnection({encodedInsertableStreams: true});
const pc2 = new RTCPeerConnection({encodedInsertableStreams: true});
pc1.onicecandidate = (e) => pc2.addIceCandidate(e.candidate);
pc2.onicecandidate = (e) => pc1.addIceCandidate(e.candidate);
pc2.ontrack = (e) => {
const transform = new TransformStream({
transform: receiveTransform
});
const receiverStreams = e.receiver.createEncodedStreams();
receiverStreams.readable
.pipeThrough(transform)
.pipeTo(receiverStreams.writable);
show(e.streams[0], true);
}
navigator.mediaDevices.getUserMedia({video})
.then((stream) => {
const transceiver = pc1.addTransceiver(stream.getVideoTracks()[0], {
streams: [stream],
sendEncodings: rids.map(rid => ({rid, scalabilityMode, scaleResolutionDownBy: scaleDown[rid]})),
});
const {sender} = transceiver;
const senderStreams = sender.createEncodedStreams();
const senderTransformStream = new TransformStream({
transform: sendTransform
});
senderStreams.readable
.pipeThrough(senderTransformStream)
.pipeTo(senderStreams.writable);
show(stream, false);
return pc1.setLocalDescription();
})
.then(() => {
return pc2.setRemoteDescription({
type: 'offer',
sdp: splitLayers(pc1.localDescription.sdp, {
disableTransportCC: remb,
rids,
streamIds,
}),
});
})
.then(() => pc2.setLocalDescription())
.then(() => {
return pc1.setRemoteDescription({
type: 'answer',
sdp: mergeLayers(pc2.localDescription.sdp, pc1.localDescription.sdp, {
disableTransportCC: remb,
rids,
streamIds,
}),
});
})
.then(() => {
window.setInterval(() => {
draw(pc1, pc2);
}, 2000);
})
.catch(e => console.error(e));
</script>
</body>
</html>