-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
275 lines (243 loc) · 8.88 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Webrtc demo</title>
<style>
.container {
display: flex;
}
.stream-container {
flex: 2;
}
.panel {
flex: 1;
}
.stream {
position: relative;
display: inline-block;
}
.tag {
position: absolute;
top: 0;
right: 0;
background: orange;
width: 140px;
height: 30px;
color: white !important;
text-align: center;
line-height: 30px;
font-size: 20px;
}
.setting {
margin-bottom: 10px;
}
.setting_title {
width: 100px;
display: inline-block;
}
.setting_input {
width: 300px;
display: inline-block;
height: 30px;
}
#submit {
background-color: #d84a38;
border: none;
border-radius: 2px;
color: white;
font-family: 'Roboto', sans-serif;
font-size: 0.8em;
margin: 0 0 1em 0;
padding: 0.5em 0.7em 0.6em 0.7em;
width: 380px;
font-size: 16px;
margin-top: 10px;
margin-left: 20px;
}
.info__content {
border: 1px solid gray;
}
</style>
</head>
<body>
<h1>Webrtc Demo: Connection through self-hosted TURN server</h1>
<h2>This is for testing self-hosted TURN server. Key in your TURN server and auth method. We will generate ICE list
and try to establish relay only Webrtc connection</h2>
<div class="container">
<div class="stream-container">
<div class="stream">
<p class="tag">source</p>
<video muted autoplay loop src="/media/sample.mp4" id="source" width="400px" height="300px"></video>
</div>
<div class="stream">
<p class="tag">destination</p>
<video muted autoplay loop id="dest" width="400px" height="300px"></video>
</div>
</div>
<div class="panel">
<h2> TURN Server setting </h2>
<div class="setting">
<label class="setting_title">host</label>
<input id="host" type="url" class="setting_input" />
</div>
<div class="setting">
<label class="setting_title">auth type</label>
<select id="auth" class="setting_input">
<option value="lt-cred">long term credential</option>
<option value="st-cred">short term credential</option>
<option value="no-auth">no auth</option>
</select>
</div>
<div class="setting">
<label class="setting_title">username</label>
<input class="setting_input" id="username" type="text" placeholder="username" />
</div>
<div class="setting">
<label class="setting_title" class="setting_title">secret</label>
<input class="setting_input" id="credential" type="text"
placeholder="password (long term) or shared secret (short term)" />
</div>
<button id="submit">Start</button>
<div class="info">
<p class="info__title">ICE Server List</p>
<pre id="ice-list" class="info__content prettyprint"></pre>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsSHA/3.1.2/sha.js"
integrity="sha512-H2dtmDos4UMqJa28vmoUWGKjQ5mvlyA6vN4J9u4PH8HCJ4aCIst9pzEbzoSwcSAjM5jtgz9+ayDR2Z8gu2vHTg=="
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
<script>
const prevConfig = readSetting();
if (prevConfig) {
const hostEle = document.getElementById("host");
const authEle = document.getElementById("auth");
const usernameEle = document.getElementById("username");
const credentialEle = document.getElementById("credential");
hostEle.value = prevConfig.host;
authEle.value = prevConfig.auth;
usernameEle.value = prevConfig.username;
credentialEle.value = prevConfig.credential;
}
const submitEle = document.getElementById("submit");
submitEle.addEventListener('click', async () => {
const iceServers = generateIceList();
const iceListEle = document.getElementById("ice-list");
iceListEle.innerText = JSON.stringify(iceServers, null, 4);
await establishConnection(iceServers);
})
function generateIceList() {
const hostEle = document.getElementById("host");
const authEle = document.getElementById("auth");
const usernameEle = document.getElementById("username");
const credentialEle = document.getElementById("credential");
const host = hostEle.value;
const auth = authEle.value;
let username = usernameEle.value;
let credential = credentialEle.value;
if (!host) {
return alert("No host");
}
if (auth !== 'no-auth' && (username === '' || credential === '')) {
return alert("Please key in username and credential");
}
saveSetting({
host,
auth,
username,
credential,
})
if (auth === 'st-cred') {
({ username, credential } = getTURNCredentials(username, credential));
}
const iceList = [{
urls: `stun:${host}`,
username,
credential
}, {
urls: `turn:${host}`,
username,
credential
}]
return iceList;
}
function getTURNCredentials(name, secret) {
var unixTimeStamp = parseInt(Date.now() / 1000) + 24 * 3600, // this credential would be valid for the next 24 hours
username = [unixTimeStamp, name].join(':'),
password;
const shaObj = new jsSHA("SHA-1", "TEXT", {
hmacKey: { value: secret, format: "TEXT" },
});
shaObj.update(username);
const hmac = shaObj.getHash("B64");
return {
username: username,
credential: hmac
};
}
async function establishConnection(iceServers) {
const pc1 = new RTCPeerConnection({
iceServers,
iceTransportPolicy: 'relay'
})
const pc2 = new RTCPeerConnection({
iceServers,
iceTransportPolicy: 'relay'
})
pc1.onicecandidate = async e => {
if (!e.candidate) return;
await pc2.addIceCandidate(e.candidate);
};
pc2.onicecandidate = async e => {
if (!e.candidate) return;
await pc1.addIceCandidate(e.candidate);
};
pc2.ontrack = (event) => {
const destEle = document.getElementById("dest");
if (event.track.kind === 'video') {
destEle.srcObject = event.streams[0];
}
};
const sourceEle = document.getElementById("source");
const stream = sourceEle.captureStream();
stream.getTracks().forEach(track => {
pc1.addTrack(track, stream)
});
const offer = await pc1.createOffer({
offerToReceiveAudio: 1,
offerToReceiveVideo: 1
});
await pc1.setLocalDescription(offer);
await pc2.setRemoteDescription(offer);
const answer = await pc2.createAnswer();
await pc1.setRemoteDescription(answer);
await pc2.setLocalDescription(answer);
}
// for convenience, save config to local storage
const storageKey = "webrtc-demo";
function readSetting() {
const result = localStorage.getItem("webrtc-demo");
if (result) {
return JSON.parse(result);
}
return null;
}
function saveSetting({
host,
auth,
username,
credential,
}) {
localStorage.setItem("webrtc-demo", JSON.stringify({
host,
auth,
username,
credential,
}));
}
</script>
</body>
</html>