-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
187 lines (171 loc) · 5.93 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="P2P WebRTC Chat With Manual HandShake"
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no" />
<title>P2P WebRTC(manual handshake)</title>
<style type="text/css">
.msg_sent{
color:blue;
}
</style>
<link rel="icon" href="favicon.ico">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script>
// Add a global error event listener early on in the page load, to help ensure that browsers
// which don't support specific functionality still end up displaying a meaningful message.
window.addEventListener('error', function(error) {
console.error(error);
console.log(error.message + ' (Your browser may not support this feature.)');
error.preventDefault();
});
</script>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div>
<h4><a title="You can open other new Stream in Browser as new tab" href="https://stefanache.github.io/p2p_webrtc_handshake/" target="_blank">My Local Stream</a></h4>
<br/>
<span id="microphone_used" style="right:0;position:fixed;"></span>
</div>
<div>
<p>
<video width=250 id="vdo_local" controls autoplay></video>
Local
<textarea rows="5" cols="50" id="txt_local_Offer"></textarea>
<button id="btn_local_Offer_Set">Create-Offer</button>
</p>
</div>
<div>
<p>
<video width=250 id="vdo_remote" controls autoplay></video>
Remote
<textarea id="txt_remote_Offer" rows="5" cols="50" placeholder="Paste remote SDP"></textarea>
<button id="btn_remote_Offer_Get">Answer</button>
</p>
</div>
<div id="dv_chat" style="overflow:scroll;">
<!--messages area -->
</div>
<div>
<br><br>
<form action="" onsubmit="event.preventDefault();return frm_send_Msg();">
<input id="inp_send_Txt" type="text" name="inp_send_Txt" placeholder="please chat here" size="50">
<button type="submit">send</button>
</form>
</div>
<script type="text/javascript">
var handshake_config = { iceServers: [{"urls":"stun:stun.l.google.com:19302"}] };
var pc = new RTCPeerConnection(handshake_config);
var local_Stream;
var chat_Enabled=true;
var context;
var source;
var _chat_Channel;
function err_Handler(err) {
console.log('err:\n',err);
}
function chat_Channel(e) {
_chat_Channel.onopen = function(e) {
console.log('_chat_Channel is open', e);
}
_chat_Channel.onmessage = function(e) {
dv_chat.innerHTML = dv_chat.innerHTML + "<pre>" + e.data + "</pre>"
}
_chat_Channel.onclose = function() {
console.log('_chat-Channel closed');
}
}
btn_local_Offer_Set.onclick = function() {
if (chat_Enabled) {
_chat_Channel = pc.createDataChannel('chat-Channel');
chat_Channel(_chat_Channel);
}
pc.createOffer().then(des => {
console.log('create-Offer ok ');
pc.setLocalDescription(des).then(
() => {
setTimeout(function() {
if (pc.iceGatheringState == "complete") {
return;
} else {
console.log('after Gether-Timeout');
txt_local_Offer.value = JSON.stringify(pc.localDescription);
/* copy Local-Description into clipboard */
txt_local_Offer.select();
document.execCommand('copy');
}
}, 2000);
console.log('set-Local-Description ok');
}
).catch(err_Handler);
// For chat
}).catch(err_Handler);
}
btn_remote_Offer_Get.onclick = function() {
var _remote_Offer = new RTCSessionDescription(JSON.parse(txt_remote_Offer.value));
console.log('remote-Offer \n', _remote_Offer);
pc.setRemoteDescription(_remote_Offer).then(
function() {
console.log('set-Remote-Description ok');
if (_remote_Offer.type == "offer") {
pc.createAnswer().then(
function(description) {
console.log('create-Answer 200 ok \n', description);
pc.setLocalDescription(description).then(
function() {
/* not used */
}
).catch(err_Handler);
}
).catch(err_Handler);
}
}
).catch(err_Handler);
}
function frm_send_Msg() {
var msg_text = inp_send_Txt.value;
dv_chat.innerHTML = dv_chat.innerHTML+"<pre class='msg_sent'>"+msg_text+"</pre>";
_chat_Channel.send(msg_text);
inp_send_Txt.value = "";
return false;
}
navigator.mediaDevices.getUserMedia({ audio: true, video: true }).then(stream => {
local_Stream = stream;
//Display the microphone used
microphone_used.innerHTML = local_Stream.getAudioTracks()[0].label;
pc.addStream(stream);
vdo_local.srcObject = stream;
vdo_local.muted = true;
}).catch(err_Handler);
pc.ondatachannel = function(e) {
if (e.channel.label == "chat-Channel") {
console.log('_chat-Channel Received -', e);
_chat_Channel = e.channel;
chat_Channel(e.channel);
}
};
pc.onicecandidate = function(e) {
var e_cand = e.candidate;
if (!e_cand) {
console.log('ice-Gathering-State complete\n', pc.localDescription.sdp);
txt_local_Offer.value = JSON.stringify(pc.localDescription);
} else {
console.log('canditate:\n',e_cand.candidate);
}
}
pc.oniceconnectionstatechange = function() {
console.log('ice-connection-state-change: ', pc.iceConnectionState);
}
pc.ontrack = function (e){
console.log('remote-onTrack', e.streams);
vdo_remote.srcObject = e.streams[0];
}
pc.onconnection = function(e) {
console.log('onConnection ', e);
}
</script>
</body>
</html>