-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
364 lines (316 loc) · 9.67 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
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
#!/usr/bin/env node
const { S3Client } = require("@aws-sdk/client-s3");
const { Upload } = require("@aws-sdk/lib-storage");
const AudioRecorder = require("node-audiorecorder");
const screenshot = require("screenshot-desktop");
const NodeWebcam = require("node-webcam");
const { program } = require("commander");
const { PassThrough } = require("stream");
// CLI configuration
program
.requiredOption("--endpoint <endpoint>", "S3 endpoint")
.requiredOption("--region <region>", "S3 region")
.requiredOption("--key <key>", "AWS access key")
.requiredOption("--secret <secret>", "AWS secret key")
.option(
"--screenshot-interval <interval>",
"Screenshot interval in ms",
"1000",
)
.option(
"--webcam-interval <interval>",
"Webcam capture interval in ms",
"1000",
)
.option("--enable-screenshot", "Enable screenshot capture", false)
.option("--enable-webcam", "Enable webcam capture", false)
.option("--webcam-device <device>", "Webcam device name")
.option("--image-quality <quality>", "Image quality (1-100)", "80")
.parse(process.argv);
const opts = program.opts();
// Initialize S3 client
const s3Client = new S3Client({
endpoint: opts.endpoint,
region: opts.region,
credentials: {
accessKeyId: opts.key,
secretAccessKey: opts.secret,
},
forcePathStyle: true,
});
// Initialize webcam
const webcamOptions = {
width: 1280,
height: 720,
quality: parseInt(opts.imageQuality),
delay: 0,
saveShots: false,
output: "buffer",
device: opts.webcamDevice,
callbackReturn: "buffer",
verbose: false,
};
class EfficientRecorder {
constructor() {
this.isRecording = false;
this.currentStream = null;
this.monitorStream = null;
this.lowQualityRecorder = null;
this.highQualityRecorder = null;
this.silenceTimer = null;
this.recordingChunks = [];
this.screenshotInterval = null;
this.webcamInterval = null;
this.webcam = null;
this.uploadQueue = [];
this.isUploading = false;
this.setupRecorders();
}
setupRecorders() {
// Low quality recorder for detection (8kHz)
this.lowQualityRecorder = new AudioRecorder(
{
program: "rec",
rate: 8000,
channels: 1,
silence: 0,
thresholdStart: 0.5,
thresholdStop: 0.5,
keepSilence: true,
},
console,
);
// High quality recorder for actual recording (44.1kHz)
this.highQualityRecorder = new AudioRecorder(
{
program: "rec",
rate: 44100,
channels: 2,
silence: 0,
thresholdStart: 0,
thresholdStop: 0,
keepSilence: true,
},
console,
);
// Setup webcam if enabled
if (opts.enableWebcam) {
this.webcam = NodeWebcam.create(webcamOptions);
// Promisify the capture method
this.captureWebcam = () => {
return new Promise((resolve, reject) => {
this.webcam.capture("", (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
};
}
}
async start() {
console.log("Starting efficient recorder...");
this.startMonitoring();
if (opts.enableScreenshot) {
this.startScreenshotCapture();
}
if (opts.enableWebcam) {
this.startWebcamCapture();
}
// Start the upload processor
this.processUploadQueue();
}
startWebcamCapture() {
const interval = parseInt(opts.webcamInterval);
console.log(`Starting webcam capture with interval: ${interval}ms`);
this.webcamInterval = setInterval(async () => {
try {
const imageBuffer = await this.captureWebcam();
this.queueUpload(imageBuffer, "webcam");
} catch (error) {
console.error("Error capturing webcam:", error);
}
}, interval);
}
startScreenshotCapture() {
const interval = parseInt(opts.screenshotInterval);
console.log(`Starting screenshot capture with interval: ${interval}ms`);
this.screenshotInterval = setInterval(async () => {
try {
const screenshotBuffer = await screenshot({ format: "png" });
this.queueUpload(screenshotBuffer, "screenshot");
} catch (error) {
console.error("Error capturing screenshot:", error);
}
}, interval);
}
queueUpload(buffer, type) {
this.uploadQueue.push({
buffer,
type,
timestamp: new Date().toISOString(),
});
}
async processUploadQueue() {
while (true) {
if (this.uploadQueue.length > 0 && !this.isUploading) {
this.isUploading = true;
const item = this.uploadQueue.shift();
try {
await this.uploadImage(item.buffer, item.type, item.timestamp);
} catch (error) {
console.error(`Error processing upload for ${item.type}:`, error);
}
this.isUploading = false;
}
await new Promise((resolve) => setTimeout(resolve, 100)); // Small delay to prevent CPU hogging
}
}
async uploadImage(buffer, type, timestamp) {
try {
const key = `${type}-${timestamp}.png`;
const upload = new Upload({
client: s3Client,
params: {
Bucket: "recordings",
Key: key,
Body: buffer,
ContentType: type === "webcam" ? "image/jpeg" : "image/png",
},
queueSize: 4,
partSize: 1024 * 1024 * 5, // 5MB parts
});
const result = await upload.done();
console.log(`${type} upload completed:`, result.Key);
} catch (error) {
console.error(`Error uploading ${type}:`, error);
}
}
startMonitoring() {
// Start the low quality recorder and get its stream
this.lowQualityRecorder.start();
this.monitorStream = this.lowQualityRecorder.stream();
// Process the monitor stream
this.monitorStream.on("data", (chunk) => {
// Convert Buffer to Int16Array for proper audio sample reading
const samples = new Int16Array(chunk.buffer);
// Calculate RMS of the audio buffer to estimate dB level
const rms = Math.sqrt(
Array.from(samples).reduce((sum, value) => sum + value * value, 0) /
samples.length,
);
const db = 20 * Math.log10(rms);
if (db > 50 && !this.isRecording) {
this.startRecording();
} else if (db <= 50 && this.isRecording) {
if (!this.silenceTimer) {
this.silenceTimer = setTimeout(() => {
this.stopRecording();
}, 2000);
}
} else if (db > 50 && this.silenceTimer) {
clearTimeout(this.silenceTimer);
this.silenceTimer = null;
}
});
this.monitorStream.on("error", (error) => {
console.error("Error in monitoring stream:", error);
});
}
startRecording() {
if (this.isRecording) return;
console.log("Starting high-quality recording...");
this.startTime = Date.now();
this.isRecording = true;
this.recordingChunks = [];
// Start high quality recording and get its stream
this.highQualityRecorder.start();
this.currentStream = this.highQualityRecorder.stream();
// Collect chunks of audio data
this.currentStream.on("data", (chunk) => {
this.recordingChunks.push(chunk);
});
// Handle any errors in the recording stream
this.currentStream.on("error", (err) => {
console.error("Error in recording stream:", err);
});
}
async stopRecording() {
if (!this.isRecording) return;
const duration = (Date.now() - this.startTime) / 1000;
console.log("Stopping recording... Duration:", duration, "seconds");
this.isRecording = false;
this.silenceTimer = null;
// Stop the recorder
this.highQualityRecorder.stop();
// Wait a bit for any final chunks
await new Promise((resolve) => setTimeout(resolve, 500));
try {
// Combine all chunks into a single buffer
const completeBuffer = Buffer.concat(this.recordingChunks);
console.log(`Total recording size: ${completeBuffer.length} bytes`);
// Create and start the upload
const timestamp = new Date().toISOString();
const key = `recording-${timestamp}.wav`;
const upload = new Upload({
client: s3Client,
params: {
Bucket: "recordings",
Key: key,
Body: completeBuffer,
ContentType: "audio/wav",
},
queueSize: 4,
partSize: 1024 * 1024 * 5, // 5MB parts
});
const result = await upload.done();
console.log("Audio upload completed successfully:", result.Key);
// Clean up
this.currentStream = null;
this.recordingChunks = [];
} catch (err) {
console.error("Error completing audio upload:", err);
throw err;
}
}
async cleanup() {
if (this.screenshotInterval) {
clearInterval(this.screenshotInterval);
}
if (this.webcamInterval) {
clearInterval(this.webcamInterval);
if (this.webcam) {
this.webcam.clear();
}
}
if (this.isRecording) {
await this.stopRecording();
}
if (this.lowQualityRecorder) {
this.lowQualityRecorder.stop();
}
// Wait for any pending uploads to complete
while (this.uploadQueue.length > 0 || this.isUploading) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
}
}
// Start the recorder
const recorder = new EfficientRecorder();
recorder.start();
// Handle cleanup on exit
process.on("SIGINT", async () => {
console.log("Cleaning up...");
await recorder.cleanup();
process.exit(0);
});
// Handle uncaught errors
process.on("uncaughtException", async (error) => {
console.error("Uncaught exception:", error);
await recorder.cleanup();
process.exit(1);
});
process.on("unhandledRejection", async (error) => {
console.error("Unhandled rejection:", error);
await recorder.cleanup();
process.exit(1);
});