-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.html
167 lines (130 loc) · 5.39 KB
/
index.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
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
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
"use strict";
function RestSignaler(signalUrl) {
this.sendOffer = async function (offer) {
return (await fetch(signalUrl, {
method: 'POST',
body: JSON.stringify(offer),
headers: { 'Content-Type': 'application/json' }
})).json();
}
}
</script>
<script type="text/javascript">
"use strict";
const STUN_SERVER = "stun:stun.sipsorcery.com";
const NOISE_FRAME_WIDTH = 80;
const NOISE_FRAME_HEIGHT = 60;
const ICE_GATHERING_TIMEOUT = 3000;
var pc;
var signaler;
var isClosed = true;
var isOfferSent = false;
window.onload = () => {
let host = window.location.hostname.concat(window.location.port ? `:${window.location.port}` : "");
document.getElementById('signalingUrl').value = `${window.location.protocol}//${host}/offer`;
};
async function start() {
if (!isClosed) {
// Close any existing peer connection.
await closePeer();
}
isOfferSent = false;
let signalingUrl = document.getElementById('signalingUrl').value;
// Create the noise.
let noiseStm = whiteNoise(NOISE_FRAME_WIDTH, NOISE_FRAME_HEIGHT);
document.getElementById("localVideoCtl").srcObject = noiseStm;
signaler = new RestSignaler(signalingUrl);
// Create the peer connections.
pc = createPeer("echoVideoCtl", noiseStm);
pc.onicegatheringstatechange = async () => {
console.log(`onicegatheringstatechange: ${pc.iceGatheringState}.`);
if (pc.iceGatheringState === 'complete') {
sendOffer();
}
}
let offer = await pc.createOffer();
await pc.setLocalDescription(offer);
setTimeout(sendOffer, ICE_GATHERING_TIMEOUT);
}
async function sendOffer() {
if (!isOfferSent) {
isOfferSent = true;
var offerWithIce = pc.localDescription;
console.log(offerWithIce);
var answer = await signaler.sendOffer(offerWithIce);
if (answer != null) {
console.log(answer.sdp)
await pc.setRemoteDescription(answer);
}
else {
console.log("Failed to get an answer from the Echo Test server.")
pc.close();
}
}
}
function createPeer(videoCtlID, noiseStm) {
console.log("Creating peer ...");
isClosed = false;
let pc = new RTCPeerConnection({ iceServers: [{ urls: STUN_SERVER }] });
noiseStm.getTracks().forEach(track => pc.addTrack(track, noiseStm));
pc.ontrack = evt => document.getElementById(videoCtlID).srcObject = evt.streams[0];
pc.onicecandidate = evt => evt.candidate && console.log(evt.candidate);
// Diagnostics.
pc.onicegatheringstatechange = () => console.log(`onicegatheringstatechange: ${pc.iceGatheringState}.`);
pc.oniceconnectionstatechange = () => console.log(`oniceconnectionstatechange: ${pc.iceConnectionState}.`);
pc.onsignalingstatechange = () => console.log(`onsignalingstatechange: ${pc.signalingState}.`);
pc.onconnectionstatechange = () => console.log(`onconnectionstatechange: ${pc.connectionState}.`);
return pc;
}
async function closePeer() {
console.log("Closing...")
isClosed = true;
await pc?.close();
};
function whiteNoise(width, height) {
const canvas = Object.assign(document.createElement("canvas"), { width, height });
const ctx = canvas.getContext('2d');
ctx.fillRect(0, 0, width, height);
const p = ctx.getImageData(0, 0, width, height);
requestAnimationFrame(function draw() {
if (!isClosed) {
for (var i = 0; i < p.data.length; i++) {
p.data[i++] = Math.random() * 255;
p.data[i++] = Math.random() * 255;
p.data[i++] = Math.random() * 255;
}
ctx.putImageData(p, 0, 0);
requestAnimationFrame(draw);
}
});
return canvas.captureStream();
}
</script>
</head>
<body>
<table>
<thead>
<tr>
<th>Source</th>
<th>Echo</th>
</tr>
</thead>
<tr>
<td>
<video controls autoplay="autoplay" id="localVideoCtl" width="320" height="240"></video>
</td>
<td>
<video controls autoplay="autoplay" id="echoVideoCtl" width="320" height="240"></video>
</td>
</tr>
</table>
<div>
<label>Signaling URL:</label> <input type="text" id="signalingUrl" size="40" /><br />
<button type="button" class="btn btn-success" onclick="start();">Start</button>
<button type="button" class="btn btn-success" onclick="closePeer();">Close</button>
</div>
</body>