-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
131 lines (104 loc) · 3.85 KB
/
main.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
const startButton = document.getElementById('startButton');
const hangupButton = document.getElementById('hangupButton');
const localVideo = document.getElementById('localVideo');
const remoteVideo = document.getElementById('remoteVideo');
const notifications = document.getElementById('notifications');
const callerIdElement = document.getElementById('callerId');
const acceptCallButton = document.getElementById('acceptCallButton');
let localStream;
let remoteStream;
let peerConnection;
const servers = {
iceServers: [
{ urls: 'stun:stun.l.google.com:19302' }
]
};
startButton.addEventListener('click', startCall);
hangupButton.addEventListener('click', hangUp);
acceptCallButton.addEventListener('click', acceptCall);
let socket = new WebSocket('ws://localhost:8080');
socket.onmessage = async (message) => {
const data = JSON.parse(message.data);
if (data.offer) {
callerIdElement.textContent = `${data.callerId} is calling...`;
notifications.classList.remove('hidden');
peerConnection = new RTCPeerConnection(servers);
peerConnection.onicecandidate = event => {
if (event.candidate) {
socket.send(JSON.stringify({ candidate: event.candidate }));
}
};
peerConnection.onaddstream = event => {
remoteStream = event.stream;
remoteVideo.srcObject = remoteStream;
};
await peerConnection.setRemoteDescription(data.offer);
} else if (data.answer) {
await peerConnection.setRemoteDescription(data.answer);
} else if (data.candidate) {
try {
await peerConnection.addIceCandidate(data.candidate);
} catch (e) {
console.error('Error adding received ice candidate', e);
}
} else if (data.hangup) {
endCall();
}
};
async function startCall() {
startButton.disabled = true;
hangupButton.disabled = false;
try {
localStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
localVideo.srcObject = localStream;
peerConnection = new RTCPeerConnection(servers);
peerConnection.onicecandidate = event => {
if (event.candidate) {
socket.send(JSON.stringify({ candidate: event.candidate }));
}
};
peerConnection.onaddstream = event => {
remoteStream = event.stream;
remoteVideo.srcObject = remoteStream;
};
peerConnection.addStream(localStream);
const offer = await peerConnection.createOffer();
await peerConnection.setLocalDescription(offer);
socket.send(JSON.stringify({ offer, callerId: 'Your ID' }));
} catch (error) {
console.error('Error accessing media devices.', error);
}
}
async function acceptCall() {
notifications.classList.add('hidden');
try {
localStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
localVideo.srcObject = localStream;
peerConnection.addStream(localStream);
const answer = await peerConnection.createAnswer();
await peerConnection.setLocalDescription(answer);
socket.send(JSON.stringify({ answer }));
} catch (error) {
console.error('Error accessing media devices.', error);
}
}
function hangUp() {
socket.send(JSON.stringify({ hangup: true }));
endCall();
}
function endCall() {
if (peerConnection) {pp
peerConnection.close();
peerConnection = null;
}
hangupButton.disabled = true;
startButton.disabled = false;
if (localVideo.srcObject) {
localVideo.srcObject.getTracks().forEach(track => track.stop());
}
if (remoteVideo.srcObject) {
remoteVideo.srcObject.getTracks().forEach(track => track.stop());
}
localVideo.srcObject = null;
remoteVideo.srcObject = null;
}