-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.html
306 lines (263 loc) · 9.4 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
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
<!DOCTYPE html>
<html>
<head>
<title>WebRTC Audio/Video-Chat</title>
</head>
<body>
<div id="app">
<span id="myid"> </span>
<video id="selfview" autoplay></video>
<video id="remoteview" autoplay></video>
<button id="endCall" style="display: none;" onclick="endCurrentCall()">End Call </button>
<div id="list">
<ul id="users">
</ul>
</div>
</div>
</body>
</html>
<script src="https://js.pusher.com/4.1/pusher.min.js"></script>
<script>
var pusher = new Pusher('XXX-APP-KEY', {
cluster: 'XXX-APP-CLUSTER',
encrypted: true,
authEndpoint: 'pusher/auth'
});
var usersOnline, id, users = [],
sessionDesc,
currentcaller, room, caller, localUserMedia;
const channel = pusher.subscribe('presence-videocall');
channel.bind('pusher:subscription_succeeded', (members) => {
//set the member count
usersOnline = members.count;
id = channel.members.me.id;
document.getElementById('myid').innerHTML = ` My caller id is : ` + id;
members.each((member) => {
if (member.id != channel.members.me.id) {
users.push(member.id)
}
});
render();
})
channel.bind('pusher:member_added', (member) => {
users.push(member.id)
render();
});
channel.bind('pusher:member_removed', (member) => {
// for remove member from list:
var index = users.indexOf(member.id);
users.splice(index, 1);
if(member.id==room){
endCall();
}
render();
});
function render() {
var list = '';
users.forEach(function(user) {
list += `<li>` + user + ` <input type="button" style="float:right;" value="Call" onclick="callUser('` + user + `')" id="makeCall" /></li>`
})
document.getElementById('users').innerHTML = list;
}
//To iron over browser implementation anomalies like prefixes
GetRTCPeerConnection();
GetRTCSessionDescription();
GetRTCIceCandidate();
prepareCaller();
function prepareCaller(){
//Initializing a peer connection
caller = new window.RTCPeerConnection();
//Listen for ICE Candidates and send them to remote peers
caller.onicecandidate = function(evt) {
if (!evt.candidate) return;
console.log("onicecandidate called");
onIceCandidate(caller, evt);
};
//onaddstream handler to receive remote feed and show in remoteview video element
caller.onaddstream = function(evt) {
console.log("onaddstream called");
if (window.URL) {
document.getElementById("remoteview").srcObject = evt.stream;
} else {
document.getElementById("remoteview").src = evt.stream;
}
};
}
function getCam() {
//Get local audio/video feed and show it in selfview video element
return navigator.mediaDevices.getUserMedia({
video: true,
audio: true
});
}
function GetRTCIceCandidate() {
window.RTCIceCandidate = window.RTCIceCandidate || window.webkitRTCIceCandidate ||
window.mozRTCIceCandidate || window.msRTCIceCandidate;
return window.RTCIceCandidate;
}
function GetRTCPeerConnection() {
window.RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection ||
window.mozRTCPeerConnection || window.msRTCPeerConnection;
return window.RTCPeerConnection;
}
function GetRTCSessionDescription() {
window.RTCSessionDescription = window.RTCSessionDescription || window.webkitRTCSessionDescription ||
window.mozRTCSessionDescription || window.msRTCSessionDescription;
return window.RTCSessionDescription;
}
//Create and send offer to remote peer on button click
function callUser(user) {
getCam()
.then(stream => {
if (window.URL) {
document.getElementById("selfview").src = window.URL.createObjectURL(stream);
} else {
document.getElementById("selfview").src = stream;
}
toggleEndCallButton();
caller.addStream(stream);
localUserMedia = stream;
caller.createOffer().then(function(desc) {
caller.setLocalDescription(new RTCSessionDescription(desc));
channel.trigger("client-sdp", {
"sdp": desc,
"room": user,
"from": id
});
room = user;
});
})
.catch(error => {
console.log('an error occured', error);
})
};
function endCall(){
room = undefined;
caller.close();
for (let track of localUserMedia.getTracks()) { track.stop() }
prepareCaller();
toggleEndCallButton();
}
function endCurrentCall(){
channel.trigger("client-endcall", {
"room": room
});
endCall();
}
//Send the ICE Candidate to the remote peer
function onIceCandidate(peer, evt) {
if (evt.candidate) {
channel.trigger("client-candidate", {
"candidate": evt.candidate,
"room": room
});
}
}
function toggleEndCallButton(){
if(document.getElementById("endCall").style.display == 'block'){
document.getElementById("endCall").style.display = 'none';
}else{
document.getElementById("endCall").style.display = 'block';
}
}
//Listening for the candidate message from a peer sent from onicecandidate handler
channel.bind("client-candidate", function(msg) {
if(msg.room==room){
console.log("candidate received");
caller.addIceCandidate(new RTCIceCandidate(msg.candidate));
}
});
//Listening for Session Description Protocol message with session details from remote peer
channel.bind("client-sdp", function(msg) {
if(msg.room == id){
console.log("sdp received");
var answer = confirm("You have a call from: "+ msg.from + "Would you like to answer?");
if(!answer){
return channel.trigger("client-reject", {"room": msg.room, "rejected":id});
}
room = msg.room;
getCam()
.then(stream => {
localUserMedia = stream;
toggleEndCallButton();
if (window.URL) {
document.getElementById("selfview").srcObject = evt.stream;
} else {
document.getElementById("selfview").src = stream;
}
caller.addStream(stream);
var sessionDesc = new RTCSessionDescription(msg.sdp);
caller.setRemoteDescription(sessionDesc);
caller.createAnswer().then(function(sdp) {
caller.setLocalDescription(new RTCSessionDescription(sdp));
channel.trigger("client-answer", {
"sdp": sdp,
"room": room
});
});
})
.catch(error => {
console.log('an error occured', error);
})
}
});
//Listening for answer to offer sent to remote peer
channel.bind("client-answer", function(answer) {
if(answer.room==room){
console.log("answer received");
caller.setRemoteDescription(new RTCSessionDescription(answer.sdp));
}
});
channel.bind("client-reject", function(answer) {
if(answer.room==room){
console.log("Call declined");
alert("call to " + answer.rejected + "was politely declined");
endCall();
}
});
channel.bind("client-endcall", function(answer) {
if(answer.room==room){
console.log("Call Ended");
endCall();
}
});
</script>
<style>
video {
/* video border */
border: 1px solid #ccc;
padding: 20px;
margin: 10px;
border-radius: 20px;
/* tranzitionstransitions applied to the vodeovideo element */
-moz-transition: all 1s ease-in-out;
-webkit-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
#list ul {
list-style: none;
}
#list ul li {
font-family: Georgia, serif, Times;
font-size: 18px;
display: block;
width: 300px;
height: 28px;
background-color: #333;
border-left: 5px solid #222;
border-right: 5px solid #222;
padding-left: 10px;
text-decoration: none;
color: #bfe1f1;
}
#list ul li:hover {
-moz-transform: rotate(-5deg);
-moz-box-shadow: 10px 10px 20px #000000;
-webkit-transform: rotate(-5deg);
-webkit-box-shadow: 10px 10px 20px #000000;
transform: rotate(-5deg);
box-shadow: 10px 10px 20px #000000;
}
</style>