-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
634 lines (602 loc) · 23.9 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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script
src="https://cdn.socket.io/4.6.0/socket.io.min.js"
integrity="sha384-c79GN5VsunZvi+Q/WObgk2in0CbZsHnjEqvFxC5DxHn9lTfNce2WW6h2pH6u/kF+"
crossorigin="anonymous"
></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="/static/js/lib/mediasoup-client.min.js"></script>
<script src="/static/js/common/constants/SocketEvents.js"></script>
<script src="/static/js/common/constants/CallState.js"></script>
<script src="/static/js/common/constants/CallType.js"></script>
<script src="/static/js/common/constants/Constants.js"></script>
<script src="/static/js/common/constants/CallElements.js"></script>
<script src="/static/js/common/constants/CallProtocol.js"></script>
<script src="/static/js/core/classes/CallEventListener.js"></script>
<script src="/static/js/core/classes/User.js"></script>
<script src="/static/js/core/classes/CallPayload.js"></script>
<script src="/static/js/core/classes/CallClient.js"></script>
<script src="/static/js/core/classes/SFUApplication.js"></script>
<script>
const { RTCPeerConnection, RTCSessionDescription } = window;
const socket = io("https://localhost", {
transports: ["websocket"],
autoConnect: true,
reconnectionDelay: 500,
reconnectionDelayMax: 1000,
randomizationFactor: 0,
reconnection: true,
timeout: 30000,
extraHeaders: {
Connection: "Upgrade",
Upgrade: "websocket",
secure: true,
},
});
socket.onAny((e, d) => console.log(e, d));
class Application {
static instance = null;
static getInstance = () => {
return Application.instance;
};
constructor() {
if (Application.instance)
throw Error("Can't create more than one instance of Application");
this.callClient = new CallClient(socket);
this.localVideoStream = null;
this.remoteVideoStream = null;
this.removeAudioStream = null;
this.sfu = new SFUApplication();
this.peerConnection = new RTCPeerConnection({
"iceServers": [
{
"credential": "",
"urls": ["stun:stun.l.google.com:19302"],
"username": ""
},
{
"credential": "",
"urls": ["stun:43.239.223.10:3478"],
"username": ""
},
{
"credential": "peerjsp",
"urls": ["turn:us-0.turn.peerjs.com:3478"],
"username": "peerjs"
},
{
'urls': ["turn:43.239.223.10:3478"],
'credential': "Tuananh050901",
'username': "Tuananh05",
}
],
"sdpSemantics": "unified-plan"
});
this.initConnection();
}
initConnection() {
this.peerConnection.onnegotiationneeded = (e) => {
if (this.peerConnection.signalingState != "stable") return;
};
this.peerConnection.ontrack = (event) => {
const remoteVideo = document.getElementById(
CallElements.remoteVideo
);
if (remoteVideo) {
remoteVideo.srcObject = event.streams[0];
}
this.callClient.markCallAsReady();
};
this.peerConnection.onicecandidate = (event) => {
if (event.candidate) {
this.callClient.sendCandidate(event.candidate);
}
};
}
setRemoteStream(track, kind) {
const remoteVideo = document.getElementById(
CallElements.remoteVideo
);
if (remoteVideo) {
if (!this.remoteVideoStream) {
this.remoteVideoStream = new MediaStream();
remoteVideo.srcObject = this.remoteVideoStream;
this.callClient.markCallAsReady();
}
this.remoteVideoStream.addTrack(track);
}
}
loginWithToken = (token) => {
this.callClient.login(id);
};
loginWithId = (id, callback) => {
this.callClient.login(id, callback);
};
getRouterCapabilities() {
return this.callClient.getRouterCapabilities();
}
initMediaObjects = async () => {
this.localVideoStream = await navigator.mediaDevices.getUserMedia(
Constants.VIDEO_CONSTRAINTS
);
console.log(this.localVideoStream.getTracks());
const localVideo = document.getElementById(CallElements.localVideo);
localVideo.srcObject = this.localVideoStream;
const tracks = this.localVideoStream.getTracks();
tracks.forEach((track) => {
this.peerConnection.addTrack(track, this.localVideoStream);
});
};
getMediaObjects = async () => {
this.localVideoStream = await navigator.mediaDevices.getUserMedia(
Constants.VIDEO_CONSTRAINTS
);
console.log(this.localVideoStream.getTracks());
const localVideo = document.getElementById(CallElements.localVideo);
localVideo.srcObject = this.localVideoStream;
}
registerElements = () => {
this.ui = {
call: {
toggleCamera: document.querySelector("#callclient__button_toggle_camera"),
cameraOn: document.querySelector("#callclient__icon_video_enabled"),
cameraOff: document.querySelector("#callclient__icon_video_disabled"),
toggleMic: document.querySelector("#callclient__button_toggle_mic"),
micOn: document.querySelector("#callclient__icon_mic_enabled"),
micOff: document.querySelector("#callclient__icon_mic_disabled"),
end: document.querySelector("#callclient__button_end_call"),
remoteVideo: document.querySelector("#callclient__remote_video_stream"),
callStatus: document.querySelector("#callclient__text_call_status"),
remoteMicOff: document.querySelector("#callclient__icon_remote_mic_disabled"),
remoteCameraOff: document.querySelector("#callclient__icon_remote_camera_disabled"),
},
caller: {
userName: document.querySelector("#callclient__caller_name")
},
callee: {
userName: document.querySelector("#callclient__callee_name"),
avatar: document.querySelector("#callclient__callee_avatar"),
background: document.querySelector("#callclient__callee_background"),
}
}
};
registerFunctions = () => {
this.ui.call.end.addEventListener("click", () => {
this.callClient.endCall();
});
this.ui.call.toggleMic.addEventListener("click", () => {
this.toggleMic();
});
this.ui.call.toggleCamera.addEventListener("click", () => {
this.toggleCamera();
});
};
setDisplay = (elem, isVisible) => {
if (isVisible) {
elem.classList.remove("hidden");
} else {
elem.classList.add("hidden");
}
}
createOffer = async () => {
const offer = await this.peerConnection.createOffer();
console.log("createOffer", offer);
await this.peerConnection.setLocalDescription(
new RTCSessionDescription(offer)
);
return offer;
};
createAnswer = async (webrtcOffer) => {
console.log("createAnswer", webrtcOffer);
await this.peerConnection.setRemoteDescription(
new RTCSessionDescription(webrtcOffer)
);
const answer = await this.peerConnection.createAnswer();
await this.peerConnection.setLocalDescription(
new RTCSessionDescription(answer)
);
return answer;
};
onReceiveAnswer = async (answer) => {
console.log("onReceiveAnswer", answer);
await this.peerConnection.setRemoteDescription(
new RTCSessionDescription(answer)
);
};
addIceCandidate = async (iceCandidate) => {
this.peerConnection.addIceCandidate(
new RTCIceCandidate(iceCandidate)
);
};
resetCall = () => {
const remoteVideo = document.getElementById(CallElements.remoteVideo);
if (remoteVideo) {
remoteVideo.srcObject = null;
}
};
changeCameraStatus(enabled) {
const tracks = this.localVideoStream.getVideoTracks();
tracks.forEach((track) => {
track.enabled = enabled;
});
}
changeMicStatus(enabled) {
const tracks = this.localVideoStream.getAudioTracks();
tracks.forEach((track) => {
track.enabled = enabled;
});
}
toggleCamera() {
const enabled = !this.callClient.videoStatus;
this.callClient.changeCameraStatus(enabled);
this.setDisplay(this.ui.call.cameraOn, enabled);
this.setDisplay(this.ui.call.cameraOff, !enabled);
}
toggleMic() {
const enabled = !this.callClient.audioStatus;
this.callClient.changeMicStatus(enabled);
this.setDisplay(this.ui.call.micOn, enabled);
this.setDisplay(this.ui.call.micOff, !enabled);
}
getCallUser = async (userId) => {
let res = await axios.post("https://skvideocall.timviec365.vn/api/getCallUser", {userId: userId});
return res.data;
}
}
window.onload = () => {
const URL_PARAMS = new URLSearchParams(window.location.search);
const CALLEE_ID = URL_PARAMS.get("calleeId");
const USER_ID = URL_PARAMS.get("userId");
const CALL_TYPE = URL_PARAMS.get("callType");
const ACCEPT_INCOMING = URL_PARAMS.get("accept");
const ACCESS_TOKEN = URL_PARAMS.get("token");
const CALL_PROTOCOL = URL_PARAMS.get("protocol");
Application.instance = new Application();
let app = Application.getInstance();
app.registerElements();
app.callClient.addCallStateListener((state) => {
if (state == CallState.CALLING) {
app.setDisplay(app.ui.call.callStatus, false);
}
console.log("callState", state);
});
app.callClient.mediaEvents.addListener((data) => {
app.setDisplay(app.ui.call.remoteMicOff, !data.audio);
app.setDisplay(app.ui.call.remoteCameraOff, !data.video);
app.setDisplay(app.ui.call.remoteVideo, data.video);
})
app.callClient.payloadEvents.addListener((payload) => {
app.ui.callee.userName.innerText = payload.calleeName;
if (payload.calleeAvatar) {
// app.ui.callee.avatar.src = payload.calleeAvatar;
// app.ui.callee.background.src = payload.calleeAvatar;
}
})
app.initMediaObjects().then(() => {
app.getCallUser(USER_ID).then(caller => {
app.ui.caller.userName.innerText = caller.userName;
app.loginWithId(caller, (data) => {
app.getRouterCapabilities().then(routerParams => {
const {capabilities} = routerParams;
console.log(capabilities);
app.sfu.loadDevice(capabilities).then(()=>{
if (CALLEE_ID) {
console.log(CALLEE_ID);
if (data.serviceStatus.sfuAvailable) {
if (CALL_PROTOCOL == "peers") {
app.getCallUser(CALLEE_ID).then(callee => {
app.callClient.createCall(
callee,
CALL_TYPE ? CALL_TYPE : CallType.VIDEO
);
});
} else {
app.getCallUser(CALLEE_ID).then(callee => {
app.callClient.createCall(
callee,
CALL_TYPE ? CALL_TYPE : CallType.VIDEO,
CallProtocol.SFU
);
});
}
} else {
app.getCallUser(CALLEE_ID).then(callee => {
app.callClient.createCall(
callee,
CALL_TYPE ? CALL_TYPE : CallType.VIDEO
);
});
}
} else if (ACCEPT_INCOMING && ACCEPT_INCOMING == 1) {
app.callClient.acceptNextCall();
app.callClient.acceptCall();
}
});
});
});
app.registerFunctions();
})
});
};
</script>
<link rel="stylesheet" type="text/css" href="/static/css/styles.css" />
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
crossorigin="anonymous"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css"
/>
</head>
<body>
<div class="main">
<div class="callarea">
<div class="callarea_content">
<div class="callvideo">
<div class="people">
<div class="peo_recieve_container">
<video
class="peo_receive_video"
id="callclient__remote_video_stream"
autoplay
></video>
<div class="image_background">
<img
id="callclient__callee_background"
src="/static/image/avatar.webp"
alt="image_background"
/>
<div class="image_avatar">
<img
id="callclient__callee_avatar"
src="/static/image/avatar.webp"
alt="avatar"
width="250"
height="250"
/>
</div>
<div class="status_call_container">
<span id="callclient__text_call_status" class="status_call">Đang kết nối ...</span>
<span id="callclient__icon_remote_mic_disabled" class="status_media hidden">
<i class="fa-solid fa-microphone-slash"
styles="color:white !important"
></i></span>
<span id="callclient__icon_remote_camera_disabled" class="status_media hidden">
<i class="fa-solid fa-video-slash"></i>
</span>
</div>
</div>
<div class="peo_receive_name">
<span id="callclient__callee_name" class="name"></span>
</div>
</div>
<div class="peo_call_container">
<video
class="peo_call_video"
id="callclient__local_video_stream"
autoplay
muted
></video>
<div class="peo_call_name">
<span id="callclient__caller_name" class="name"></span>
</div>
</div>
</div>
</div>
<div class="callnormal hidden">
<div class="image_background">
<div class="image_avatar">
<img id="avatar" src="/static/image/naruto.jpg" alt="avatar" />
</div>
<span class="status_call">Đang kết nối ...</span>
</div>
</div>
</div>
<div class="callarea_bottom">
<button class="btn">
<i class="fa-solid fa-circle-user fa-lg"></i>
</button>
<div class="group_btn_center">
<div class="btn_video">
<button class="btn" id="callclient__button_toggle_camera">
<div id="callclient__icon_video_enabled">
<i class="fa-solid fa-video"></i>
</div>
<div class="hidden" id="callclient__icon_video_disabled">
<i class="fa-solid fa-video-slash"></i>
</div>
</button>
<button class="btn">
<i class="fa-solid fa-chevron-right fa-rotate-270"></i>
</button>
</div>
<button class="btn btn-danger btn-circle" id="callclient__button_end_call">
<i class="fa-solid fa-phone"></i>
</button>
<div class="btn_mic">
<button class="btn" id="callclient__button_toggle_mic">
<div id="callclient__icon_mic_enabled">
<i class="fa-solid fa-microphone"></i>
</div>
<div class="hidden" id="callclient__icon_mic_disabled">
<i
class="fa-solid fa-microphone-slash"
styles="color:white !important"
></i>
</div>
</button>
<button
class="btn"
type="button"
data-bs-toggle="dropdown"
aria-expanded="false"
>
<i class="fa-solid fa-chevron-right fa-rotate-270"></i>
</button>
<ul class="dropdown-menu">
<li>
<b><i class="fa-solid fa-microphone"></i> Chọn micro</b>
</li>
<li><i class="fa-solid fa-check"></i> Microphone Array</li>
<li>
<i class="fa-solid fa-check"></i> Thiết bị mặc đinh
</li>
<hr />
<li>
<b><i class="fa-solid fa-volume-high"></i>  Chọn loa</b>
</li>
<li>
<i class="fa-solid fa-check"></i> Speaker (Realtek (R)
Audio)
</li>
<li>
<i class="fa-solid fa-check"></i> Thiết bị mặc đinh
</li>
<hr />
<li>
<div
class="btn_settings"
data-bs-toggle="modal"
data-bs-target="#exampleModal"
>
  Mở cài đặt
</div>
</li>
</ul>
</div>
</div>
<button
class="btn"
data-bs-toggle="modal"
data-bs-target="#exampleModal"
>
<i class="fa-solid fa-gear fa-lg"></i>
</button>
<div
class="modal fade"
id="exampleModal"
tabindex="-1"
aria-labelledby="exampleModalLabel"
aria-hidden="true"
>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title fs-5" id="exampleModalLabel">
Tình trạng thiết bị
</h5>
<button
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
></button>
</div>
<div class="modal-body">
<div class="alert alert-primary alert_custom" role="alert">
<div>
<i class="fa-solid fa-video"></i> Camera đang hoạt
động
</div>
<i class="fa-solid fa-circle-check"></i>
</div>
<div class="alert alert-danger alert_custom" role="alert">
<div>
<i class="fa-solid fa-video"></i> Camera không hoạt
động
</div>
<i class="fa-solid fa-circle-xmark"></i>
</div>
<label>Camera</label>
<select
class="form-select"
aria-label="Default select example"
>
<option selected>Intergrated Webcam</option>
<option value="1">One</option>
</select>
<div class="camera_test_container">
<video class="camera_test" autoplay muted></video>
<div class="camera_test_fail">
<i class="fa-solid fa-video-slash"></i>
</div>
</div>
<div class="alert alert-primary alert_custom" role="alert">
<div>
<i class="fa-solid fa-microphone"></i> Mic đang hoạt
động
</div>
<i class="fa-solid fa-circle-check"></i>
</div>
<div class="alert alert-danger alert_custom" role="alert">
<div>
<i class="fa-solid fa-microphone"></i> Mic không hoạt
động
</div>
<i class="fa-solid fa-circle-xmark"></i>
</div>
<label>Micro</label>
<select
class="form-select"
aria-label="Default select example"
>
<option selected>Thiết bị mặc định</option>
<option value="1">One</option>
</select>
<div class="alert alert-primary alert_custom" role="alert">
<div>
<i class="fa-solid fa-microphone"></i> Loa/tai nghe đang hoạt
động
</div>
<i class="fa-solid fa-circle-check"></i>
</div>
<div class="alert alert-danger alert_custom" role="alert">
<div>
<i class="fa-solid fa-microphone"></i> Loa/tai nghe không hoạt
động
</div>
<i class="fa-solid fa-circle-xmark"></i>
</div>
<label>Loa/tai nghe</label>
<select
class="form-select"
aria-label="Default select example"
>
<option selected>Thiết bị mặc định</option>
<option value="1">One</option>
</select>
<label>Âm lượng</label>
<label>Âm lượng</label>
<input class="" type="range" style="width: 100%" />
</div>
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-secondary"
data-bs-dismiss="modal"
>
Huỷ
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"
></script>
</body>
</html>