forked from tejaswigowda/mjpeg-to-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
123 lines (111 loc) · 3.89 KB
/
server.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
var fs = require('fs');
var path = require('path');
var http = require('http');
var MjpegCamera = require('mjpeg-camera');
var WriteStream = require('stream').Writable;
var program = require('commander');
var unpipe = require('unpipe');
program
.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 [3000]', '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 || 3000;
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, {'Access-Control-Allow-Origin' : '*' ,'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, {'Access-Control-Allow-Origin' : '*', '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, {'Access-Control-Allow-Origin' : '*', '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>');
*/
var filePath = './public' + req.url;
if (filePath == './public/')
filePath = './public/index.html';
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.json':
contentType = 'application/json';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpg':
contentType = 'image/jpg';
break;
case '.wav':
contentType = 'audio/wav';
break;
}
fs.readFile(filePath, function(error, content) {
if (error) {
if(error.code == 'ENOENT'){
fs.readFile('./public/404.html', function(error, content) {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
});
}
else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
res.end();
}
}
else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
}
}).listen(port);