-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnano.html
executable file
·435 lines (377 loc) · 16.5 KB
/
nano.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
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
<!DOCTYPE html>
<html>
<head>
<!--ARM端和电脑连接的情况下的布局设计-->
<meta content="text/html ;chaset=utf-8" http-equiv="content-type">
<title>WebRTC-Demo</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico"/>
<style>
video.tempwindow{
width:533pt;
height:400pt;
border:1px solid dodgerblue;
box-shadow:0px 3px 6px 1px skyblue;
}
video.yourwindow{
width:534pt;
height:400pt;
border:1px solid dodgerblue;
box-shadow:0px 3px 6px 1px skyblue;
left:355pt;
}
video.ownwindow{
width:180pt;
top:354px;
right: 122px;
position: fixed;
}
textarea{
width : 320px;
height : 240px;
border : 1px solid dodgerblue;
box-shadow : 0px 3px 6px 1px skyblue;
}
p.title {
font-size:40px;
}
p.tag {
font-size:50px;
}
button{
font-size:40px;
}
</style>
</head>
<body>
<script></script>
<script src="extra/adapter.js" type="text/javascript"></script>
<script src="clientXHRSignalingChannel.js" type="text/javascript"></script>
<!--<script src="https://webrtc.github.io/adapter/adapter.js"
type = "text/javascript"></script>-->
<script>
var signalingChannel,key,id,
haveLocalMedia = false,
weWaited = false,
myVideoStream,myVideo,tempVideo,
yourVideoStream,yourVideo,
doNothing = function(){},
pc, dc , data={},
constraints = {mandatory:{
OfferToReceiveAudio : true,
OfferToReceiveVideo : true
}};
var videoconstraints = {
audio: true,
video: true
};
//below is main function
window.onload = function(){
if(queryparams && queryparams['key']){
document.getElementById("key").value = queryparams['key'];
connect();
}
myVideo = document.getElementById("myVideo");
tempVideo = document.getElementById("tempVideo");
yourVideo = document.getElementById("yourVideo");
getMedia();
}//end onload function
//now will create signaling channel
function connect(){
var errorCB , scHandlers,handleMsg;
//console.log("connect");
//get key from Element key.value
key = document.getElementById("key").value;
//display message from signaling channel
handleMsg = function(msg){
//release message to screen
var msgE = document.getElementById("inmessages");
var msgString = JSON.stringify(msg);
msgE.value = msgString + "\n" +msgE.value;
if(msg.type=== "offer"){
pc.setRemoteDescription(new RTCSessionDescription(msg));
answer();
}else if(msg.type === "answer"){
pc.setRemoteDescription(new RTCSessionDescription(msg));
}else if(msg.type === "candidate"){
pc.addIceCandidate(
new RTCIceCandidate({sdpMLineIndex:msg.mlineindex,
candidate:msg.candidate}));
}
};
//signaling channel handle function
scHandlers = {
'onWaiting' : function(){
setStatus("Waiting");
weWaited = true;
},
'onConnected': function(){
setStatus("Connected");
//wait for ready of local media
createPC();
},
'onMessage' : handleMsg
};
//finally, create signaling channel
signalingChannel = createSignalingChannel(key,scHandlers);
errorCB = function(msg){
document.getElementById("response").innerHTML = msg;
};
//connect to serverXHRSignalingChannel
//console.log("signaling channel connect",typeof(signalingChannel));
signalingChannel.connect(errorCB);
}//end connect function
function send(msg){
var handler = function(res){
document.getElementById("response").innerHTML=res;
return;
},
msg=msg||document.getElementById("message").value,
//release to screen
msgE=document.getElementById("outmessages");
var msgString = JSON.stringify(msg).replace(/\\r\\n/g,'\n');
msgE.value = msgString +"\n" +msgE.value;
//send message by signaling channel
signalingChannel.send(msg,handler);
}//end send function
//abtain local Media
function getMedia(){
navigator.mediaDevices.getUserMedia(videoconstraints)
.then(gotUserMedia)
.catch(didntGetUserMedia);
}
function gotUserMedia(stream){
myVideoStream = stream;
haveLocalMedia=true;
myVideo.srcObject=myVideoStream;
tempVideo.srcObject=myVideoStream;
attachMediaIfReady();
}
function didntGetUserMedia(){
console.log("Couldn't get video");
}
//set up RTC peer connect
function createPC(){
var stunuri = true,
turnuri = false,
myfalse = function(v){
return ((v==="0"||(v==="false")||(!v)));
},
config = new Array();
if(queryparams){
if('stunuri'in queryparams){
stunuri = !myfalse(queryparams['stunuri']);
}
if('turnuri' in queryparams){
turnuri = !myfalse(queryparams['turnuri']);
};
};
if(stunuri){
//this server is google public stun server
config.push({"url":"stun:stun.l.google.com:19302"});
}
if(turnuri){
if(stunuri){
config.push({"url":"turn:user@turn.webrtcbook.com",
"credential":"test"});
}else{
config.push({"url":"turn:user@turn-only.webrtcbook.com",
"credential":"test"});
}
}
console.log("config = "+JSON.stringify(config));
pc = new RTCPeerConnection({iceServers:config});
pc.onicecandidate = onIceCandidate;
pc.onaddstream = onRemoteStreamAdded;
pc.onremovestream = onRemoteStreamRemoved;
pc.ondatachannel = onDataChannelAdded;
attachMediaIfReady();
}//end createPC
function onIceCandidate(e){
if(e.candidate){
send({"type":'candidate',
mlineindex : e.candidate.sdpMLineIndex,
candidate : e.candidate.candidate});
}
}
function onRemoteStreamAdded(e){
yourVideoStream = e.stream;
yourVideo.srcObject=yourVideoStream;
setStatus("On call");
}
function onRemoteStreamRemoved(e){}
//if another bower add data channel
//this function will save data channel id an set handle function
function onDataChannelAdded(e){
dc = e.channel;
setupDataHandlers();
sendChat("hello");
}
//datachannel handle function
function setupDataHandlers(){
data.send=function(msg){
msg = JSON.stringify(msg);
console.log("sending "+msg + " over data channel");
dc.send(msg);
}
dc.onmessage = function(e){
var msg = JSON.parse(e.data),
cb = document.getElementById("chatbox"),
rtt = document.getElementById("rtt");
if(msg.rtt){
console.log("received rtt of '"+msg.rtt + "'");
rtt.value = msg.rtt;
msg = msg.rtt;
}else if (msg.chat){
console.log("received chat of '"+msg.chat + "'");
cb.value +="<- "+msg.chat +"\n";
rtt.value = "";
cb.scrollTop = cb.scrollHeight;
msg = msg.chat;
}else{
console.log("received "+msg +"on data channel");
}
};
}
function sendRtt(){
var msg = document.getElementById("chat").value;
data.send({'rtt':msg});
}
function sendChat(msg){
var cb = document.getElementById("chatbox"),
c = document.getElementById("chat");
msg = msg || c.value;
console.log("sendChat(" + msg + ")");
cb.value += "->" +msg + "\n";
data.send({'chat':msg});
c.value = '';
cb.scrollTop = cb.scrollHeight;
}
function attachMediaIfReady(){
if(pc && haveLocalMedia){attachMedia();};
}
//add local meida stream to peer connection
function attachMedia(){
pc.addStream(myVideoStream);
setStatus("Ready for call");
if(queryparams && queryparams['call'] && !weWaited){
call();
}
}
//create SDP for offer
function call(){
dc= pc.createDataChannel('chat');
setupDataHandlers();
pc.createOffer(gotDescription,doNothing,constraints);
}
//create SDP for answer
function answer(){
pc.createAnswer(gotDescription,doNothing,constraints);
}
//set Local Session Description and send it to peer brower
function gotDescription(localDesc){
pc.setLocalDescription(localDesc);
send(localDesc);
}
//display the message of signaling
function setStatus(str){
var statusLineE=document.getElementById("statusline"),
statusE = document.getElementById("status"),
sendE = document.getElementById("send"),
callE = document.getElementById("call");
connectE = document.getElementById("connect"),
scMessage = document.getElementById("scMessage");
switch(str){
case "Waiting":
statusLineE.style.display = "inline";
statusE.innerHTML =
"Waiting for peer signaling connection";
sendE.style.display="none";
connectE.style.display = "none";
break;
case "Connected":
statusLineE.style.display = "inline";
statusE.innerHTML =
"Peer signaling connected,Waiting for local Media";
// sendE.style.display="inline";
connectE.style.display="none";
// scMessage.style.display = "inline-block";
break;
case "Ready for call":
statusE.innerHTML = "Ready for call";
callE.style.display="inline";
break;
case "On call":
statusE.innerHTML = "On call";
callE.style.display = "none";
tempVideo.style.display = "none";
yourVideo.style.display = "inline";
myVideo.style.display = "inline";
break;
default:
}
}
</script>
<div id = "setup" align = "center";>
<p class = "title">WebRTC Demo (local media and signaling, peer connection and data channel)</p>
<p class = "tag">Key:
<input type = "text" name="key" value = "xuin" id = "key" style="font-size:40px; width: 100;"
onkeyup ="if(event.keyCode==13){
connect; return false;
}"/>
<button id = "connect" onclick="connect();" >Connect</button>
<button id="call" style="display : none;"
onclick="call()">Call</button>
<br>
<span id = "statusline" style="display:none; font-size:30px;">Status:
<span id = "status">Disconnected</span>
</span>
</p>
</div>
<div align = "center";>
<video class="tempwindow" id="tempVideo" autoplay = "autoplay" muted="false" />
</div>
<div align = "center";>
<video class="yourwindow" id="yourVideo" autoplay="autoplay" style="display: none"/></div>
</div>
<div align = "center";>
<video class="ownwindow" id="myVideo" autoplay = "autoplay" muted="false" style="display: none"/>
</div>
<div id = "scMessage" style="float:right;display:none;display:none">
<p>Signaling channel message:
<input type="text" width="100%" name="message" id="message"
onkeyup="if(event.keyCode ==13){
send(); return false;
}"/>
<button id = "send" style = "display:none"
onclick="send()">Send</button>
</p>
<p>Response: <span id="response"></span></p>
</div>
<br />
<div style="display:none">
<p><b>Outgoing Message</b>
<br/>
<textarea id = "outmessages"></textarea>
</p>
</div>
<div style="width:30%;vertical-align:top;display:none">
<textarea id = "chatbox" rows="10" style = "width:100%"></textarea>
<p style="width:100%"><b>Real-time:</b>
<textarea id = "rtt" rows = "2" style="width:100%"></textarea>
</p>
<p style="width:100%"><b>Chat message:</b>
<input type="text" style="width:100%" name="chat" id="chat"
onkeyup="sendRtt();
if(event.keyCode == 13){
sendChat(); return false;
}"/>
</p>
</div>
<div style="width:30%;vertical-align:top;display:none">
<p><b>Incoming Message</b>
<br />
<textarea id="inmessages"></textarea>
</p>
</div>
</body>
</html>