-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
98 lines (84 loc) · 3.36 KB
/
Copy pathmain.js
File metadata and controls
98 lines (84 loc) · 3.36 KB
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
const localVideo = document.getElementById('localVideo');
const remoteVideo = document.getElementById('remoteVideo');
let localStream;
let remoteStream;
let peerConnection;
const socket = io.connect(window.location.origin);
socket.on('message', message => {
console.log('Message received:', message);
// Handle signaling messages received from the server
});
async function start() {
try {
localStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
localVideo.srcObject = localStream;
createPeerConnection(); // Initialize peer connection
} catch (error) {
console.error('Error accessing media devices.', error);
}
}
async function call() {
console.log("Connecting");
document.getElementById('callButton').innerText = 'Connecting';
createPeerConnection();
const offer = await peerConnection.createOffer();
await peerConnection.setLocalDescription(offer);
socket.emit('message', {'description': offer});
}
async function stopCall() {
document.getElementById('callButton').innerText = 'Start Call';
peerConnection.close();
peerConnection = null;
remoteVideo.srcObject = null;
socket.emit('message', { type: 'disconnectCall' });
}
const configuration = {'iceServers': [{'urls': 'stun:stun.l.google.com:19302'}]}; // STUN server
// Initialize peer connection
function createPeerConnection() {
peerConnection = new RTCPeerConnection(configuration);
// Handle ICE candidates
peerConnection.onicecandidate = event => {
if (event.candidate) {
console.log('Sending ICE candidate', event.candidate);
socket.emit('message', {'candidate': event.candidate});
}
};
// When remote stream is added
peerConnection.ontrack = event => {
remoteVideo.srcObject = event.streams[0];
console.log('Remote stream added.');
};
// Add local stream tracks to peer connection
localStream.getTracks().forEach(track => {
peerConnection.addTrack(track, localStream);
console.log('Adding local stream tracks to the peer connection');
});
peerConnection.oniceconnectionstatechange = event => {
if (peerConnection.iceConnectionState === 'disconnected' ||
peerConnection.iceConnectionState === 'failed' ||
peerConnection.iceConnectionState === 'closed') {
console.log('Peer disconnected. Clearing remote video.');
remoteVideo.srcObject = null;
}
};
}
// Listen for signaling messages from server
socket.on('message', async message => {
if(message.type === 'disconnectCall'){
stopCall();
}
if (message.description) {
console.log('Received description', message.description);
await peerConnection.setRemoteDescription(new RTCSessionDescription(message.description));
// If received an offer, then create an answer
if (peerConnection.remoteDescription.type === 'offer') {
const answer = await peerConnection.createAnswer();
await peerConnection.setLocalDescription(answer);
socket.emit('message', {'description': peerConnection.localDescription});
}
} else if (message.candidate) {
console.log('Received ICE candidate', message.candidate);
await peerConnection.addIceCandidate(new RTCIceCandidate(message.candidate));
}
});
start();