-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
140 lines (117 loc) · 3.72 KB
/
server.js
File metadata and controls
140 lines (117 loc) · 3.72 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
const http = require('http');
const { spawn } = require('child_process');
const fs = require('fs');
const url = require('url');
const PORT = 8000;
const html = fs.readFileSync('./index.html');
let ffmpegProcess = null;
let camActive = false;
let streamClients = [];
function startFFmpeg() {
if (ffmpegProcess) return;
ffmpegProcess = spawn('ffmpeg', [
'-f', 'video4linux2',
'-input_format', 'mjpeg',
'-fflags', 'nobuffer',
'-flags', 'low_delay',
'-i', '/dev/video0',
'-vf', 'scale=640:480',
'-r', '15',
'-f', 'mjpeg',
'-q:v', '7',
'-an',
'-'
]);
ffmpegProcess.stdout.on('data', (chunk) => {
streamClients.forEach((res) => {
try {
res.write(`--frame\r\n`);
res.write(`Content-Type: image/jpeg\r\n`);
res.write(`Content-Length: ${chunk.length}\r\n\r\n`);
res.write(chunk);
res.write('\r\n');
} catch (err) {
console.log('Erreur écriture stream:', err.message);
}
});
});
ffmpegProcess.stderr.on('data', (data) => {
// console.error('ffmpeg stderr:', data.toString()); // décommenter pour debug
});
ffmpegProcess.on('close', (code) => {
console.log('ffmpeg stoppé avec code', code);
ffmpegProcess = null;
camActive = false;
// Fermer tous les clients stream
streamClients.forEach((res) => { try { res.end(); } catch {} });
streamClients = [];
});
camActive = true;
console.log('✅ Caméra démarrée');
}
function stopFFmpeg() {
if (ffmpegProcess) {
ffmpegProcess.kill('SIGINT');
ffmpegProcess = null;
}
camActive = false;
streamClients.forEach((res) => { try { res.end(); } catch {} });
streamClients = [];
console.log('🛑 Caméra arrêtée');
}
http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
const pathname = parsedUrl.pathname;
// CORS basique pour les requêtes fetch
res.setHeader('Access-Control-Allow-Origin', '*');
if (pathname === '/') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(html);
return;
}
// Statut de la cam
if (pathname === '/status') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ active: camActive }));
return;
}
// Activer la cam
if (pathname === '/cam/on') {
startFFmpeg();
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ active: true }));
return;
}
// Désactiver la cam
if (pathname === '/cam/off') {
stopFFmpeg();
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ active: false }));
return;
}
// Stream MJPEG
if (pathname === '/stream') {
if (!camActive) {
res.writeHead(503, { 'Content-Type': 'text/plain' });
res.end('Caméra inactive');
return;
}
res.writeHead(200, {
'Content-Type': 'multipart/x-mixed-replace; boundary=frame',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Pragma': 'no-cache'
});
console.log('Client connecté au stream');
streamClients.push(res);
req.on('close', () => {
console.log('Client déconnecté');
streamClients = streamClients.filter(c => c !== res);
});
return;
}
res.writeHead(404);
res.end();
}).listen(PORT, '0.0.0.0', () => {
console.log(`✅ Stream dispo sur http://100.127.151.6:${PORT}`);
});