forked from mmaelzer/mjpeg-camera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs
executable file
·73 lines (66 loc) · 2.43 KB
/
cs
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
#!/usr/bin/env node
var http = require('http');
var MjpegCamera = require('./mjpeg-camera');
var WriteStream = require('stream').Writable;
var program = require('commander');
var pack = require('./package.json');
var unpipe = require('unpipe');
program
.version(pack.version)
.option('-u --user []', 'Set the username for camera authentication')
.option('-pw --password []', 'Set the password for camera authentication')
.option('-l --url []', 'Set the url for the camera')
.option('-p --port [8080]', 'Set the port for the http server to listen on', parseInt)
.option('-n --name [camera]', 'Set the name of the camera')
.parse(process.argv);
if (!program.url) {
program.help();
}
// Create a new MjpegCamera object
var camera = new MjpegCamera({
user: program.user || '',
password: program.password || '',
url: program.url,
name: typeof program.name === 'function' ? '' : program.name
});
camera.start();
var boundary = '--boundandrebound';
var port = program.port || 8080;
console.log('===', camera.name, 'camera server listening on', port, '===');
http.createServer(function(req, res) {
// A request to http://localhost/stream returns an unending sequence of jpegs
// Listen for a disconnect from the client to properly unpipe the jpeg stream
if (/stream/.test(req.url)) {
res.writeHead(200, {'Content-Type': 'multipart/x-mixed-replace; boundary=' + boundary});
var ws = new WriteStream({objectMode: true});
ws._write = function(chunk, enc, next) {
var jpeg = chunk.data;
res.write(boundary + '\nContent-Type: image/jpeg\nContent-Length: '+ jpeg.length + '\n\n');
res.write(jpeg);
next();
};
camera.pipe(ws);
res.on('close', function() {
unpipe(camera);
});
}
// A request to http://localhost/frame returns a single frame as a jpeg
else if (/frame/.test(req.url)) {
res.writeHead(200, {'Content-Type': 'image/jpeg'});
res.end(camera.frame);
}
// A request to http://localhost returns a simple webpage that will render
// a livestream of jpegs from the camera
else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<!doctype html>\
<html>\
<head>\
<title>'+camera.name+'</title>\
</head>\
<body style="background:#000;">\
<img src="/stream" style="width:100%;height:auto;">\
</body>\
</html>');
}
}).listen(port);