-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
264 lines (244 loc) · 9.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
<!DOCTYPE html>
<html>
<head>
<title>WebRTC Sender</title>
<style>
#videos {
width: 100%;
text-align: center;
}
#chat {
width: 100%;
text-align: center;
}
#chat-messages {
text-align: left;
margin: 0 auto;
width: 350px;
height: 350px;
overflow-x: hidden;
overflow-y: auto;
background: #efefef;
padding: 0.5em;
box-sizing: border-box;
}
#chat-messages p {
word-break: break-all;
}
#chat-form {
margin: 0 auto;
width: 350px;
height: 50px;
}
#chat-input {
float: left;
text-align: left;
width: 250px;
height: 50px;
}
#chat-send {
float: right;
width: 90px;
height: 55px
}
video {
width: 450px;
height: 450px;
/* border: 1px #000 solid; */
}
</style>
</head>
<body>
<div id="videos">
<video playsinline autoplay="true" muted="true" id="local"></video>
</div>
<div id="chat">
<div id="chat-messages"></div>
<div id="chat-form">
<textarea id="chat-input"></textarea>
<button id="chat-send">Send</button>
</div>
</div>
<div id="outOptions">
<p>
<button disabled="disabled" id="toggleCam"></button>
<button disabled="disabled" id="toggleMicrophone"></button>
<button disabled="disabled" id="toggleAudio"></button>
</p>
<p id="roomid"></p>
<p id="errors"></p>
</div>
<script>
// For debugging purposes on mobile devices. Do not do this.
window.onerror = console.log = console.error = function(a,b,c,d,e){
document.querySelector("#errors").innerHTML = document.querySelector("#errors").innerHTML + "<br> - " + [a,b,c,d,e].join(" ");
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
<script src="./dist/PeerClient.min.js"></script>
<script>
(function(){
var peer = new PeerClient({
server: "wss://sowe.sowecms.com:8999",
streamOptions: {
video: true, microphone: true, audio: true
},
streamConstraints: {
audio: true, video: true
},
iceServers: [
{ urls: 'stun:stun2.l.google.com:19302' },
{
urls: 'turn:167.86.69.169:5349',
username: 'sowecms',
credential: 'GDOWcZL?5R4FiJ'
}
],
iceCandidatePoolSize: 40,
onLocalStream: function (stream) {
document.querySelector('video#local').srcObject = stream;
document.querySelector('#toggleCam').disabled = false;
document.querySelector('#toggleMicrophone').disabled = false;
document.querySelector('#toggleAudio').disabled = false;
},
onRemoteStreamStart: function (callId, stream) {
// For iOS compatibility, this might be called more than once
var id = "remote" + callId;
var video = document.querySelector("#" + id);
if(video === null){
video = document.createElement("video");
video.setAttribute("playsinline", true);
video.autoplay = true;
video.controls = false;
video.muted = false;
video.className = "remote";
video.id = id;
document.querySelector('#videos').appendChild(video);
}
video.srcObject = stream;
// Updating user media to apply sound muted if required
peer.updateUserMedia();
},
onRemoteStreamClose: function (callId) {
var video = document.querySelector("#remote" + callId);
document.querySelector('#videos').removeChild(video);
},
onUpdateStreamOptions: function (streamOptions) {
if (streamOptions.video) {
document.querySelector("#toggleCam").innerText = "Disable Cam";
} else {
document.querySelector("#toggleCam").innerText = "Enable Cam";
}
if (streamOptions.microphone) {
document.querySelector("#toggleMicrophone").innerText = "Disable Microphone";
} else {
document.querySelector("#toggleMicrophone").innerText = "Enable Microphone";
}
if (streamOptions.audio) {
document.querySelector("#toggleAudio").innerText = "Disable Sound";
} else {
document.querySelector("#toggleAudio").innerText = "Enable Sound";
}
},
onPermissionDenied: function (type, error) {
switch (type) {
case "camera": // Recoverable, will run only microphone.
alert("Camera permission was denied. Others won't be able to see you.");
break;
case "microphone": // Recoverable, will run only camera.
alert("Microphone permission was denied. Others won't be able to hear you.");
break;
case "both": // Nothing to do here. The end
alert("Microphone and Camera permissions were denied. We can't get you in the room.");
break;
}
},
onUnsupportedWebRTC: function () {
alert("Your browser does not support WebRTC");
},
onUserEvent: function (event) {
switch (event.type) {
case "created":
var url = location.protocol + "//" + location.hostname + "/index.html#" + event.room.id + ":";
document.querySelector("#roomid").innerHTML = "<a href='" + url + "'>" + url + "</a>";
break;
case "joined":
break;
case "left":
break;
case "kicked":
break;
case "banned":
break;
case "unbanned":
break;
}
},
onRoomEvent: function (event) {
switch (event.type) {
case "resource":
break;
case "message":
var p = document.createElement("p");
if (peer.socket.id == event.client.id)
p.innerText = "You: " + event.data;
else
p.innerText = "Other: " + event.data;
document.querySelector("#chat-messages").appendChild(p);
break;
case "joined":
break;
case "left":
break;
case "kicked":
break;
case "banned":
break;
case "unbanned":
break;
}
}
});
async function boot(){
peer.initSocket();
await peer.initUserMedia();
var data = window.location.hash.replace("#", "").split(":")
.filter(function(item){ return item.length; });
switch (data.length) {
case 0:
peer.createRoom();
case 1:
peer.joinRoom(data[0]);
break;
case 2:
peer.joinRoom(data[0], data[1]);
break;
default:
alert("Invalid link");
}
document.querySelector('#toggleCam').onclick = function () {
peer.toggleCam();
}
document.querySelector('#toggleMicrophone').onclick = function () {
peer.toggleMicrophone();
}
document.querySelector('#toggleAudio').onclick = function () {
peer.toggleAudio();
}
document.querySelector("#chat-input").addEventListener("keyup", function (event) {
if (event.keyCode === 13) {
event.preventDefault();
peer.sendMessage(document.querySelector("#chat-input").value);
document.querySelector("#chat-input").value = "";
}
});
document.querySelector("#chat-send").onclick = function () {
peer.sendMessage(document.querySelector("#chat-input").value);
document.querySelector("#chat-input").value = "";
}
}
window.onload = boot;
})();
</script>
</body>
</html>