-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
544 lines (498 loc) · 14.6 KB
/
server.js
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
var UserRegistry = require("./user-registry.js");
var UserSession = require("./user-session.js");
var ochestrive = require("./ochestrive.json");
var userRegistry = new UserRegistry();
var rooms = {};
var express = require("express");
// kurento required
var path = require("path");
var url = require("url");
var http = require("http");
var kurento = require("kurento-client");
const KURENTOURL = ochestrive.KURENTO;
// Constants
const settings = {
WEBSOCKETURL: "http://localhost:8080/",
KURENTOURL: KURENTOURL,
// KURENTOURL: "ws://localhost:8888/kurento",
};
/*
* Server startup
*/
var app = express(); // Express Sever
var asUrl = url.parse(settings.WEBSOCKETURL); //socket Server url
var port = asUrl.port; //port for socket server
// 지정된 포트로 http 서버를 연다
var server = app.listen(port, function () {
console.log("Kurento Tutorial started");
console.log("Open " + url.format(asUrl) + " with a WebRTC capable browser");
});
var io = require("socket.io")(server);
/**
* Message handlers
*/
io.on("connection", (socket) => {
//Client의 소켓이 연결되면 일어나는 이벤트
var userList = "";
// 유저 레지스트리에 저장된 유저들을 출력합니다
// 근데 보통 유저 혼자 들어온다
for (var userId in userRegistry.usersById) {
userList += " " + userId + ",";
}
console.log(
"receive new client : " + socket.id + " currently have : " + userList
);
socket.emit("id", socket.id); // 클라이언트에게 Socket id를 보낸다
socket.on("error", function (data) {
//에러 발생 이벤트
console.log("Connection: " + socket.id + " error : " + data);
leaveRoom(socket.id, function () {}); // 유저들을 방에서 탈출 시킨다
});
socket.on("disconnect", function (data) {
// socket 연결 끊겼을 때
console.log("Connection: " + socket.id + " disconnect : " + data);
leaveRoom(socket.id, function () {
// 방을 떠난다. 이거 자세히 알아보도록
var userSession = userRegistry.getById(socket.id); //유저의 id를 얻어서 끊어버림
stop(userSession.id);
});
});
socket.on("message", function (message) {
console.log("Connection: " + socket.id + " receive message: " + message.id);
switch (message.id) {
case "register":
//클라이언트가 register를 누르면 동작
console.log("Server : Register " + socket.id);
register(socket, message.name, function () {});
break;
case "joinRoom":
console.log(
"Server : " + socket.id + " joinRoom : " + message.roomName
);
joinRoom(socket, message.roomName, function () {});
break;
case "receiveVideoFrom":
console.log(socket.id + " receiveVideoFrom : " + message.sender);
receiveVideoFrom(
socket,
message.sender,
message.sdpOffer,
function () {}
);
break;
case "leaveRoom":
console.log(socket.id + " leaveRoom");
leaveRoom(socket.id);
break;
case "onIceCandidate":
addIceCandidate(socket, message);
break;
default:
socket.emit({
id: "error",
message: "Invalid message " + message,
});
}
});
});
/**
* Register user to server
* @param socket
* @param name
* @param callback
*/
function register(socket, name, callback) {
var userSession = new UserSession(socket.id, socket);
userSession.name = name;
userRegistry.register(userSession);
userSession.sendMessage({
id: "registered",
data: "Server : Successfully registered " + socket.id,
});
// console.log(userRegistry);
}
/*desktop 정보 확인 */
function checkDeskInfo(corenum, callback) {
console.log("checkDeskInfo : ", corenum);
var MEDIA_CONSTRAINTS = {
audio: true,
video: {
width: 640,
framerate: 30,
},
};
//i3코어이면 화질을 낮춘다?
if (corenum < 6) {
// kurento-utils에 getMedia 부분 참고하면 좋을것같다.
constraints = MEDIA_CONSTRAINTS;
navigator.getUserMedia(
constraints,
function (stream) {
videoStream = stream;
start();
},
callback
);
navigator.mediaDevices
.getUserMedia(constraints)
.then(function (stream) {
videoStream = stream;
start();
})
.catch(callback);
}
}
/**
* Gets and joins room
* @param socket
* @param roomName
* @param callback
*/
function joinRoom(socket, roomName, callback) {
const room = getRoom(roomName, function (error, room) {
if (error) {
callback(error);
}
});
join(socket, room, function (error, user) {
console.log("join success : " + user.id);
});
// console.log(room);
}
/**
* Gets room. Creates room if room does not exist
* @param roomName
* @param callback
*/
function getRoom(roomName, callback) {
let room = rooms[roomName];
if (room == null) {
console.log("create new room : " + roomName);
const kurentoClient = getKurentoClient(function (error, kurentoClient) {
if (error) {
return callback(error);
} // create pipeliRne for room
});
const pipeline = kurentoClient.create(
"MediaPipeline",
(error, pipeline) => {
if (error) {
return callback(error);
}
pipeline.setLatencyStats(true, (error) => {
if (error) {
return callback(error);
}
});
}
);
room = {
name: roomName,
pipeline: pipeline,
participants: {},
kurentoClient: kurentoClient,
};
rooms[roomName] = room;
} else {
console.log("get existing room : " + roomName);
}
callback(null, room);
return room;
}
/**
* Join (conference) call room
* @param socket
* @param room
* @param callback
*/
function join(socket, room, callback) {
// create user session
// User의 socket id로 유저의 세션을 불러옵니다.
var userSession = userRegistry.getById(socket.id);
console.log("userSesssion is " + userSession);
userSession.setRoomName(room.name);
var outgoingMedia = room.pipeline.create(
"WebRtcEndpoint",
(error, outgoingMedia) => {
if (error) {
console.error("no participant in room");
// no participants in room yet release pipeline
if (Object.keys(room.participants).length == 0) {
room.pipeline.release();
}
return callback(error);
}
}
);
outgoingMedia.setMaxVideoSendBandwidth(10000);
outgoingMedia.setMinVideoSendBandwidth(10000);
userSession.outgoingMedia = outgoingMedia;
// 엔드 포인트 만들어지기 전에 생긴 candidate를 처리한다
getIcecandidateBeforeEstablished(userSession, socket);
// candidate : IP 주소와 포트 넘버의 조합으로 표시된 주소
userSession.outgoingMedia.on("OnIceCandidate", function (event) {
console.log("generate outgoing candidate : " + userSession.id);
var candidate = kurento.register.complexTypes.IceCandidate(event.candidate);
userSession.sendMessage({
id: "iceCandidate",
sessionId: userSession.id,
candidate: candidate,
});
});
// notify other user that new user is joining
const usersInRoom = room.participants; // 방 안의 유저들을 불러온다
const data = {
id: "newParticipantArrived",
new_user_id: userSession.id,
};
// notify existing user
for (var i in usersInRoom) {
usersInRoom[i].sendMessage(data);
}
var existingUserIds = [];
for (var i in room.participants) {
existingUserIds.push(usersInRoom[i].id);
}
// send list of current user in the room to current participant
userSession.sendMessage({
id: "existingParticipants",
data: existingUserIds,
roomName: room.name,
});
// register user to room
room.participants[userSession.id] = userSession;
}
function getIcecandidateBeforeEstablished(userSession, socket) {
// add ice candidate the get sent before endpoint is established
// candidate가 어떻게 생성되는지 알면 해결할 수 있을 듯
var iceCandidateQueue = userSession.iceCandidateQueue[socket.id];
if (iceCandidateQueue) {
while (iceCandidateQueue.length) {
var message = iceCandidateQueue.shift();
console.error(
"user : " + userSession.id + " collect candidate for outgoing media"
);
console.log("icecandidate per message : " + message);
userSession.outgoingMedia.addIceCandidate(message.candidate);
}
}
}
/**
* Leave (conference) call room
* @param sessionId
* @param callback
*/
function leaveRoom(sessionId, callback) {
var userSession = userRegistry.getById(sessionId);
if (!userSession) {
return;
}
var room = rooms[userSession.roomName];
if (!room) {
return;
}
console.log(
"notify all user that " +
userSession.id +
" is leaving the room " +
room.name
);
var usersInRoom = room.participants;
delete usersInRoom[userSession.id];
userSession.outgoingMedia.release();
// release incoming media for the leaving user
for (var i in userSession.incomingMedia) {
userSession.incomingMedia[i].release();
delete userSession.incomingMedia[i];
}
var data = {
id: "participantLeft",
sessionId: userSession.id,
};
for (var i in usersInRoom) {
var user = usersInRoom[i];
// release viewer from this
user.incomingMedia[userSession.id].release();
delete user.incomingMedia[userSession.id];
// notify all user in the room
user.sendMessage(data);
}
// Release pipeline and delete room when room is empty
if (Object.keys(room.participants).length == 0) {
room.pipeline.release();
delete rooms[userSession.roomName];
}
delete userSession.roomName;
}
/**
* Unregister user
* @param sessionId
*/
function stop(sessionId) {
userRegistry.unregister(sessionId);
}
/**
* Retrieve sdpOffer from other user, required for WebRTC calls
* @param socket
* @param senderId
* @param sdpOffer
* @param callback
*/
function receiveVideoFrom(socket, senderId, sdpOffer, callback) {
var userSession = userRegistry.getById(socket.id);
var sender = userRegistry.getById(senderId);
getEndpointForUser(userSession, sender, function (error, endpoint) {
if (error) {
callback(error);
}
endpoint.processOffer(sdpOffer, function (error, sdpAnswer) {
console.log("process offer from : " + senderId + " to " + userSession.id);
if (error) {
return callback(error);
}
var data = {
id: "receiveVideoAnswer",
sessionId: sender.id,
sdpAnswer: sdpAnswer,
};
userSession.sendMessage(data);
endpoint.gatherCandidates(function (error) {
if (error) {
return callback(error);
}
});
return callback(null, sdpAnswer);
});
});
}
/**
* Get user WebRTCEndPoint, Required for WebRTC calls
* @param userSession
* @param sender
* @param callback
*/
function getEndpointForUser(userSession, sender, callback) {
// request for self media
if (userSession.id === sender.id) {
callback(null, userSession.outgoingMedia);
return;
}
var incoming = userSession.incomingMedia[sender.id];
if (incoming == null) {
// console.log(
// "user : " +
// userSession.id +
// " create endpoint to receive video from : " +
// sender.id
// );
getRoom(userSession.roomName, function (error, room) {
if (error) {
return callback(error);
}
room.pipeline.create("WebRtcEndpoint", (error, incomingMedia) => {
if (error) {
// no participants in room yet release pipeline
if (Object.keys(room.participants).length == 0) {
room.pipeline.release();
}
return callback(error);
}
// console.log(
// "user : " + userSession.id + " successfully created pipeline"
// );
incomingMedia.getStats("AUDIO", (error, statsMap) => {
if (error) {
return callback(error);
} else {
console.log(statsMap);
}
});
incomingMedia.setMaxVideoSendBandwidth(100);
incomingMedia.setMinVideoSendBandwidth(100);
userSession.incomingMedia[sender.id] = incomingMedia;
// add ice candidate the get sent before endpoint is established
var iceCandidateQueue = userSession.iceCandidateQueue[sender.id];
if (iceCandidateQueue) {
while (iceCandidateQueue.length) {
var message = iceCandidateQueue.shift();
console.log(
"user : " +
userSession.id +
" collect candidate for : " +
message.data.sender
);
incomingMedia.addIceCandidate(message.candidate);
}
}
incomingMedia.on("OnIceCandidate", function (event) {
console.log(
"generate incoming media candidate : " +
userSession.id +
" from " +
sender.id
);
var candidate = kurento.register.complexTypes.IceCandidate(
event.candidate
);
userSession.sendMessage({
id: "iceCandidate",
sessionId: sender.id,
candidate: candidate,
});
});
sender.outgoingMedia.connect(incomingMedia, function (error) {
if (error) {
callback(error);
}
callback(null, incomingMedia);
});
});
});
} else {
console.log(
"user : " +
userSession.id +
" get existing endpoint to receive video from : " +
sender.id
);
sender.outgoingMedia.connect(incoming, function (error) {
if (error) {
callback(error);
}
callback(null, incoming);
});
}
}
/**
* Add ICE candidate, required for WebRTC calls
* @param socket
* @param message
*/
function addIceCandidate(socket, message) {
var user = userRegistry.getById(socket.id);
if (user != null) {
// assign type to IceCandidate
var candidate = kurento.register.complexTypes.IceCandidate(
message.candidate
);
user.addIceCandidate(message, candidate);
} else {
console.error("ice candidate with no user receive : " + socket.id);
}
}
/**
* Retrieve Kurento Client to connect to Kurento Media Server, required for WebRTC calls
* @param callback
* @returns {*}
*/
function getKurentoClient(callback) {
return kurento(settings.KURENTOURL, function (error, kurentoClient) {
if (error) {
var message =
"Coult not find media server at address " + settings.KURENTOURL;
return callback(message + ". Exiting with error " + error);
}
callback(null, kurentoClient);
});
}
app.use(express.static(path.join(__dirname, "static")));