-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
295 lines (259 loc) · 8.89 KB
/
script.js
File metadata and controls
295 lines (259 loc) · 8.89 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
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
let currentStream = null;
let currentCameraId = null;
let isListingCameras = false;
// Helper function to get unique storage key for camera selection per stream
function getCameraStorageKey() {
const streamName = Edrys.module.stationConfig?.streamName || "Camera 1";
return `selectedCameraId_${streamName}`;
}
// Function to reload only this specific stream instance
function reloadThisStream() {
const streamName = Edrys.module.stationConfig?.streamName || "Camera 1";
Edrys.sendMessage('reload', { targetStream: streamName });
}
function applyVideoTransform(videoElement, settings) {
videoElement.style.transform = `scaleX(${
settings?.mirrorX ? -1 : 1
}) scaleY(${
settings?.mirrorY ? -1 : 1
}) rotate(${
settings?.rotate ?? 0
}deg)`;
}
// Helper function to get video constraints based on quality setting
function getVideoConstraints(deviceId, quality) {
let baseConstraints = deviceId ? { deviceId: { exact: deviceId } } : true;
if (typeof baseConstraints === 'object' && baseConstraints !== null) {
// Apply quality settings to existing constraints
switch (quality) {
case 'low':
return {
...baseConstraints,
width: { ideal: 640 },
height: { ideal: 480 },
frameRate: { ideal: 15 }
};
case 'medium':
return {
...baseConstraints,
width: { ideal: 1280 },
height: { ideal: 720 },
frameRate: { ideal: 24 }
};
case 'high':
return {
...baseConstraints,
width: { ideal: 1920 },
height: { ideal: 1080 },
frameRate: { ideal: 30 }
};
default:
return baseConstraints;
}
}
// If baseConstraints is just true or false, apply quality directly
if (baseConstraints === true) {
switch (quality) {
case 'low':
return {
width: { ideal: 640 },
height: { ideal: 480 },
frameRate: { ideal: 15 }
};
case 'medium':
return {
width: { ideal: 1280 },
height: { ideal: 720 },
frameRate: { ideal: 24 }
};
case 'high':
return {
width: { ideal: 1920 },
height: { ideal: 1080 },
frameRate: { ideal: 30 }
};
default:
return true;
}
}
return baseConstraints;
}
async function listCameras() {
if (isListingCameras) return;
isListingCameras = true;
try {
const cameraSelect = document.getElementById("camera-select");
cameraSelect.innerHTML = '';
cameraSelect.style.display = 'none';
if (!navigator.mediaDevices?.enumerateDevices) {
return;
}
currentCameraId = sessionStorage.getItem(getCameraStorageKey());
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter(device => device.kind === "videoinput");
videoDevices.forEach((device, index) => {
const option = document.createElement("option");
option.value = device.deviceId;
option.text = device.label || `Camera ${index + 1}`;
cameraSelect.appendChild(option);
});
if (videoDevices.length > 1) {
cameraSelect.style.display = 'inline-block';
}
if (currentCameraId) {
const deviceExists = videoDevices.some(device => device.deviceId === currentCameraId);
if (deviceExists) {
cameraSelect.value = currentCameraId;
} else {
sessionStorage.removeItem(getCameraStorageKey());
currentCameraId = null;
}
}
} catch (err) {
console.error("Error listing cameras:", err);
} finally {
isListingCameras = false;
}
}
function startStreamWithCamera(deviceId = null) {
const videoElement = document.getElementById("video");
const loaderElement = document.getElementById("loader");
const streamMethod = Edrys.module.stationConfig?.streamMethod || "webrtc";
const websocketUrl = Edrys.module.stationConfig?.websocketUrl || "";
const streamName = Edrys.module.stationConfig?.streamName || "Camera 1";
const streamQuality = Edrys.module.stationConfig?.streamQuality || "medium";
applyVideoTransform(videoElement, Edrys.module.stationConfig);
const startNewStream = async () => {
if (deviceId && currentCameraId !== deviceId) {
currentCameraId = deviceId;
sessionStorage.setItem(getCameraStorageKey(), deviceId);
}
const videoConstraints = Edrys.module.stationConfig?.video === undefined
? getVideoConstraints(deviceId, streamQuality)
: deviceId && Edrys.module.stationConfig?.video
? getVideoConstraints(deviceId, streamQuality)
: Edrys.module.stationConfig?.video;
const constraints = {
video: videoConstraints,
audio: Edrys.module.stationConfig?.audio === undefined
? true : Edrys.module.stationConfig?.audio,
};
try {
const stream = await navigator.mediaDevices.getUserMedia(constraints);
videoElement.srcObject = stream;
videoElement.autoplay = true;
loaderElement.classList.add("hidden");
if (currentStream && typeof currentStream.updateStream === "function") {
// Update existing stream server with new stream
currentStream.updateStream(stream);
} else {
// Create new stream server
currentStream = await Edrys.sendStream(stream, {
method: streamMethod,
websocketUrl: websocketUrl,
streamName: streamName,
});
}
// Show stream info
document.getElementById("stream-info").innerHTML = `
<div>Stream: ${streamName}</div>
<div>Quality: ${streamQuality.toUpperCase()}</div>
<div>Room: ${Edrys.liveUser?.room}</div>
`;
document.getElementById("stream-info").style.display = 'block';
} catch (error) {
console.error(error);
loaderElement.querySelector(".loader-text").textContent = "Error connecting to camera";
}
};
if (currentStream && deviceId && currentCameraId !== deviceId) {
// Just switching cameras, don't stop the stream server
startNewStream();
} else if (currentStream && (!deviceId || currentCameraId === deviceId)) {
// Stopping and restarting (e.g., on reload)
if (typeof currentStream.stop === "function") {
currentStream.stop();
}
currentStream = null;
setTimeout(startNewStream, 500);
} else {
startNewStream();
}
}
function startServer() {
if (navigator.mediaDevices?.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true })
.then(tempStream => {
tempStream.getTracks().forEach(track => track.stop());
listCameras().then(() => {
const cameraSelect = document.getElementById("camera-select");
const startWithDevice = currentCameraId ||
(cameraSelect.options.length > 0 ? cameraSelect.options[0].value : null);
startStreamWithCamera(startWithDevice);
});
})
.catch(err => {
startStreamWithCamera();
});
} else {
startStreamWithCamera();
}
const cameraSelect = document.getElementById('camera-select');
if (!cameraSelect.hasAttribute('data-listener-added')) {
cameraSelect.addEventListener('change', function() {
if (this.value) {
startStreamWithCamera(this.value);
}
});
cameraSelect.setAttribute('data-listener-added', 'true');
}
}
function startClient() {
const videoElement = document.getElementById("video");
const loaderElement = document.getElementById("loader");
const streamMethod = Edrys.module.stationConfig?.streamMethod || "webrtc";
const websocketUrl = Edrys.module.stationConfig?.websocketUrl || "";
// Add delay before starting client to allow server to initialize
setTimeout(() => {
Edrys.onStream(
(stream, settings, metadata) => {
videoElement.srcObject = stream;
applyVideoTransform(videoElement, settings);
videoElement.onloadeddata = () => {
loaderElement.classList.add("hidden");
};
if (metadata?.streamName) {
document.getElementById("stream-name").textContent = `Stream: ${metadata.streamName}`;
document.getElementById("stream-info").style.display = 'block';
}
setTimeout(() => {
loaderElement.classList.add("hidden");
}, 10000);
},
{
method: streamMethod,
websocketUrl: websocketUrl,
}
).then(streamClient => {
currentStream = streamClient;
});
}, 2000);
}
Edrys.onReady(() => {
if (Edrys.role === "station") {
startServer();
} else {
startClient();
}
});
Edrys.onMessage(({ subject, body }) => {
if (subject === "reload") {
const streamName = Edrys.module.stationConfig?.streamName || "Camera 1";
// Only reload if this message targets this specific stream instance
if (body === true || (body?.targetStream && body.targetStream === streamName)) {
setTimeout(() => {
window.location.reload();
}, Edrys.role === "station" ? 100 : 1000);
}
}
});