-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (144 loc) · 4.7 KB
/
index.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
/* eslint-disable no-undef */
const ws = new WebSocket(`wss://${window.location.host}`);
const viewBtns = document.querySelectorAll('.view-btn');
const remoteVideo = document.querySelector('.remote-video');
const camerasContainer = document.querySelector('.cameras-container');
const config = {
iceServers: [...turnIceServers, { urls: 'stun:stun.l.google.com:19302' }],
};
const loader = document.querySelector('.loader');
const logoutBtn = document.querySelector('.logout-btn');
const pc = new RTCPeerConnection(config);
let selectedCameraId;
/**
* Sets the status of the cameras by making a POST request to the '/api/camera-status' endpoint
* and updating the corresponding HTML elements with the status information.
* @param {Array} cameras - An array of camera objects.
* @returns {Promise<void>} - A Promise that resolves
* when the status of all cameras has been updated.
*/
async function setCamerasStatuses(cameras) {
try {
const response = await fetch('/api/camera-status', {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ cameras }),
});
const camerasStatuses = await response.json();
camerasStatuses.forEach((cameraStatus) => {
const cameraStatusElement = document.querySelector(`.camera-container[data-cameraId="${cameraStatus.id}"] .camera-status`);
if (cameraStatus.isConnected) {
cameraStatusElement.classList.add('connected');
} else {
cameraStatusElement.classList.remove('connected');
}
});
} catch (error) {
console.error(error);
}
}
// send a register message to the signal channel when the connection is opened
ws.addEventListener('open', () => {
ws.send(JSON.stringify({
type: 'register',
sender: user.username,
target: 'server',
clientType: 'user',
data: {
token: user.token,
inRegistrationProcess: false,
registeredCameras: cameras.map((camera) => camera.id),
},
}));
});
// setting the remoteVideo srcObject when a remote track is received
pc.ontrack = ({ track, streams }) => {
console.log('Got remote track and streams:');
if (remoteVideo.srcObject) {
return;
}
[remoteVideo.srcObject] = streams;
loader.classList.add('hide');
remoteVideo.parentElement.classList.remove('hide');
camerasContainer.classList.add('hide');
};
// sending ice candidates to the camera through the signal channel
pc.onicecandidate = ({ candidate }) => {
if (candidate) {
ws.send(JSON.stringify({
type: 'ice-candidate',
sender: user.username,
target: selectedCameraId,
clientType: 'user',
data: candidate,
}));
}
};
// sending the offer to the camera through the signal channel,
// starting the WebRTC negotiation process
viewBtns.forEach((viewBtn) => {
viewBtn.addEventListener('click', async () => {
selectedCameraId = viewBtn.parentElement.dataset.cameraid;
loader.classList.remove('hide');
viewBtn.disabled = true;
try {
pc.addTransceiver('video', { direction: 'recvonly' });
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
console.log('Sending offer to camera: ', selectedCameraId);
ws.send(JSON.stringify({
type: 'offer',
sender: user.username,
target: selectedCameraId,
clientType: 'user',
data: pc.localDescription,
}));
} catch (error) {
console.error('Error creating offer: ', error);
}
});
});
logoutBtn.addEventListener('click', async () => {
try {
const response = await fetch('/api/logout', {
method: 'POST',
credentials: 'include',
});
if (response.ok && response.redirected) {
window.location.href = response.url;
} else {
console.error('Failed to logout');
}
} catch (error) {
console.error(error);
}
});
// handling answer or ice candidates from camera
ws.addEventListener('message', async (event) => {
console.log('Got message from signaling server');
const parsedData = JSON.parse(event.data);
const { type } = parsedData;
try {
if (type === 'answer') {
const { data } = parsedData;
const answer = new RTCSessionDescription(data);
await pc.setRemoteDescription(answer);
} else if (type === 'ice-candidate') {
const { data } = parsedData;
const candidate = new RTCIceCandidate(data);
await pc.addIceCandidate(candidate);
} else {
console.warn('Unrecognized message type:', type);
}
} catch (err) {
console.error('Failed to handle message', err);
}
});
setCamerasStatuses(cameras);
// setting the status of the cameras every 10 seconds
setInterval(async () => {
await setCamerasStatuses(cameras);
}, 10000);