This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathindex.html
186 lines (171 loc) · 6.46 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
</head>
<body>
<div>
<video id="localVideo" autoplay="true" muted="muted"></video>
<video id="remoteVideo" autoplay="true" style="display:none"></video>
</div>
<script type="text/javascript">
var answer = 0;
var pc=null
var localStream=null;
var ws=null;
// Not necessary with websockets, but here I need it to distinguish calls
var unique = Math.floor(100000 + Math.random() * 900000);
var localVideo = document.getElementById('localVideo');
var remoteVideo = document.getElementById('remoteVideo');
var configuration = {
'iceServers': [
{ 'urls': 'stun:stun.stunprotocol.org:3478' },
{ 'urls': 'stun:stun.l.google.com:19302' },
//{'urls': 'stun:stun1.l.google.com:19302' },
//{'urls': 'stun:stun2.l.google.com:19302' }
]
};
// Start
navigator.mediaDevices.getUserMedia({
// audio: true, // audio is off here, enable this line to get audio too
video: true
}).then(function (stream) {
localVideo.srcObject = stream;
localStream = stream;
try {
ws = new EventSource('serverGet.php?unique='+unique);
} catch(e) {
console.error("Could not create eventSource ",e);
}
// Websocket-hack: EventSource does not have a 'send()'
// so I use an ajax-xmlHttpRequest for posting data.
// Now the eventsource-functions are equal to websocket.
ws.send = function send(message) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState!=4) {
return;
}
if (this.status != 200) {
console.log("Error sending to server with message: " +message);
}
};
xhttp.open('POST', 'serverPost.php?unique='+unique, true);
xhttp.setRequestHeader("Content-Type","Application/X-Www-Form-Urlencoded");
xhttp.send(message);
}
// Websocket-hack: onmessage is extended for receiving
// multiple events at once for speed, because the polling
// frequency of EventSource is low.
ws.onmessage = function(e) {
if (e.data.includes("_MULTIPLEVENTS_")) {
multiple = e.data.split("_MULTIPLEVENTS_");
for (x=0; x<multiple.length; x++) {
onsinglemessage(multiple[x]);
}
} else {
onsinglemessage(e.data);
}
}
// Go show myself
localVideo.addEventListener('loadedmetadata',
function () {
publish('client-call', null)
}
);
}).catch(function (e) {
console.log("Problem while getting audio/video stuff ",e);
});
function onsinglemessage(data) {
var package = JSON.parse(data);
var data = package.data;
console.log("received single message: " + package.event);
switch (package.event) {
case 'client-call':
icecandidate(localStream);
pc.createOffer({
offerToReceiveAudio: 1,
offerToReceiveVideo: 1
}).then(function (desc) {
pc.setLocalDescription(desc).then(
function () {
publish('client-offer', pc.localDescription);
}
).catch(function (e) {
console.log("Problem with publishing client offer"+e);
});
}).catch(function (e) {
console.log("Problem while doing client-call: "+e);
});
break;
case 'client-answer':
if (pc==null) {
console.error('Before processing the client-answer, I need a client-offer');
break;
}
pc.setRemoteDescription(new RTCSessionDescription(data),function(){},
function(e) { console.log("Problem while doing client-answer: ",e);
});
break;
case 'client-offer':
icecandidate(localStream);
pc.setRemoteDescription(new RTCSessionDescription(data), function(){
if (!answer) {
pc.createAnswer(function (desc) {
pc.setLocalDescription(desc, function () {
publish('client-answer', pc.localDescription);
}, function(e){
console.log("Problem getting client answer: ",e);
});
}
,function(e){
console.log("Problem while doing client-offer: ",e);
});
answer = 1;
}
}, function(e){
console.log("Problem while doing client-offer2: ",e);
});
break;
case 'client-candidate':
if (pc==null) {
console.error('Before processing the client-answer, I need a client-offer');
break;
}
pc.addIceCandidate(new RTCIceCandidate(data), function(){},
function(e) { console.log("Problem adding ice candidate: "+e);});
break;
}
};
function icecandidate(localStream) {
pc = new RTCPeerConnection(configuration);
pc.onicecandidate = function (event) {
if (event.candidate) {
publish('client-candidate', event.candidate);
}
};
try {
pc.addStream(localStream);
}catch(e){
var tracks = localStream.getTracks();
for(var i=0;i<tracks.length;i++){
pc.addTrack(tracks[i], localStream);
}
}
pc.ontrack = function (e) {
document.getElementById('remoteVideo').style.display="block";
document.getElementById('localVideo').style.display="none";
remoteVideo.srcObject = e.streams[0];
};
}
function publish(event, data) {
console.log("sending ws.send: " + event);
ws.send(JSON.stringify({
event:event,
data:data
}));
}
</script>
</body>
</html>