-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpeer_host.html
240 lines (150 loc) · 7.5 KB
/
peer_host.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
<!doctype html>
<html lang="en">
<head>
<meta charset = "utf-8" />
<title>Peer | Host</title>
<style>
canvas{
position:absolte;
left:0;
top:0;
}
</style>
</head>
<body>
<video id="localVideo" autoplay muted style="width:300px;height:200px;z-index:1000;position:absolute;top:0;left:0;"></video>
<video id="remoteVideo" autoplay style="width:300px;height:200px;display:none;"></video>
<button id="startBtn" style="position:absolute;left:800px;top:0;" type="button">Call</button>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r73/three.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/211120/orbitControls.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var localVideo;
var remoteVideo;
var peerConnection;
var peerConnectionConfig = {'iceServers': [{'url': 'stun:stun.services.mozilla.com'}, {'url': 'stun:stun.l.google.com:19302'}]};
navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia;
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
window.RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate || window.webkitRTCIceCandidate;
window.RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription;
function UUID() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
// then to call it, plus stitch in '4' in the third group
var guid = (UUID() + UUID() + "-" + UUID() + "-4" + UUID().substr(0,3) + "-" + UUID() + "-" + UUID() + UUID() + UUID()).toLowerCase();
var socket = io('https://clark-next.codio.io:9600', {secure: true});
socket.on('whatIsYourName', function () {
socket.emit('mynameis', { name: guid });
});
socket.on('clients', function (data) {
$('#clients').find('option').remove().end();
for(var k in data.clients){
if(data.clients[k] != guid){
$("#clients").append('<option value="'+data.clients[k]+'">'+data.clients[k]+'</option>').val(data.clients[k])
}
}
});
socket.on('onMessage', function (data) {
if(!peerConnection) start(false);
var signal = data;
if(signal.sdp) {
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp), function() {
if(signal.sdp.type == 'offer') {
peerConnection.createAnswer(gotDescription, createAnswerError);
}
});
} else if(signal.ice) {
peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice));
}
});
$( document ).ready(function() {
var constraints = {
video: true,
audio: true,
};
if(navigator.getUserMedia) {
navigator.getUserMedia(constraints, getUserMediaSuccess, getUserMediaError);
} else {
alert('Your browser does not support getUserMedia API');
}
localVideo = document.getElementById('localVideo');
remoteVideo = document.getElementById('remoteVideo');
$("#startBtn").click(function(){
start(true);
});
});
function getUserMediaSuccess(stream) {
var width=300;
var height=200;
localStream = stream;
localVideo.src = window.URL.createObjectURL(stream);
}
function getUserMediaError(error) {
console.log(error);
}
function start(isCaller) {
peerConnection = new RTCPeerConnection(peerConnectionConfig);
peerConnection.onicecandidate = gotIceCandidate;
peerConnection.onaddstream = gotRemoteStream;
peerConnection.addStream(localStream);
if(isCaller) {
peerConnection.createOffer(gotDescription, createOfferError);
}
}
function gotDescription(description) {
peerConnection.setLocalDescription(description, function () {
socket.emit("message",{'sdp': description});
}, function() {console.log('set description error')});
}
function gotIceCandidate(event) {
if(event.candidate != null) {
socket.emit("message",{'ice': event.candidate});
}
}
function gotRemoteStream(event) {
remoteVideo.src = window.URL.createObjectURL(event.stream);
var width = window.innerWidth;
var height = window.innerHeight;
var renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(width, height);
document.body.appendChild(renderer.domElement);
videoTexture = new THREE.Texture(remoteVideo);
videoTexture.minFilter = THREE.LinearFilter;
videoTexture.magFilter = THREE.LinearFilter;
videoTexture.format = THREE.RGBFormat;
var scene = new THREE.Scene();
var cubeGeometry = new THREE.SphereGeometry(500, 60, 40);
var sphereMat = new THREE.MeshBasicMaterial({map: videoTexture});
sphereMat.side = THREE.BackSide;
var cube = new THREE.Mesh(cubeGeometry, sphereMat);
scene.add(cube);
var camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 10000);
camera.position.y = 0;
camera.position.z = 500;
scene.add(camera);
var controls = new THREE.OrbitControls( camera);
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.enableZoom = false;
controls.maxDistance = 500;
controls.minDistance = 500;
function render() {
if( remoteVideo.readyState === remoteVideo.HAVE_ENOUGH_DATA ){
videoTexture.needsUpdate = true;
}
controls.update();
renderer.render(scene, camera);
requestAnimationFrame(render);
}
render();
}
function createOfferError(error) {
console.log(error);
}
function createAnswerError(error) {
console.log(error);
}
</script>
</body>
</html>