├── .gitignore ├── LICENSE ├── README.MD ├── camera.js ├── package.json └── raspberry-pi-mjpeg-server.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016, John Doherty 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | # raspberry-pi-mjpeg-server 2 | [![npm](https://img.shields.io/npm/dt/raspberry-pi-mjpeg-server.svg)](https://www.npmjs.com/package/raspberry-pi-mjpeg-server) 3 | 4 | Node.js mjpeg streaming server to provide video like access to the Raspberry PI camera module 5 | 6 | ## Installation 7 | 8 | ```js 9 | npm install raspberry-pi-mjpeg-server --save 10 | ``` 11 | 12 | ## Usage 13 | 14 | ``` 15 | $ node raspberry-pi-mjpeg-server.js -w 1280 -l 1024 16 | ``` 17 | 18 | ### Options 19 | 20 | ``` 21 | -p, --port port number (default 8080) 22 | -w, --width image width (default 640) 23 | -l, --height image height (default 480) 24 | -q, --quality jpeg image quality from 0 to 100 (default 85) 25 | -s, --sharpness Set image sharpness (-100 - 100) 26 | -c, --contrast Set image contrast (-100 - 100) 27 | -b, --brightness Set image brightness (0 - 100) 0 is black, 100 is white 28 | -s, --saturation Set image saturation (-100 - 100) 29 | -t, --timeout timeout in milliseconds between frames (default 500) 30 | -h, --help display this help 31 | -v, --version show version 32 | ``` 33 | 34 | ## Access the stream 35 | 36 | Open your browser and visit: 37 | 38 | ``` 39 | http://rpi-ip-address:port 40 | ``` 41 | 42 | You can get direct access to the image (HTTP multipart document) via 43 | 44 | ``` 45 | http://rpi-ip-address:port/image.jpg 46 | ``` 47 | 48 | ## License 49 | 50 | [MIT License](LICENSE) © 2016 [John Doherty](https://twitter.com/mrJohnDoherty) 51 | -------------------------------------------------------------------------------- /camera.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | module.exports = function Camera(){ 4 | 5 | this.ENCODING_JPEG = "jpg"; 6 | this.ENCODING_BMP = "bmp"; 7 | this.ENCODING_GIF = "gif"; 8 | this.ENCODING_PNG = "png"; 9 | 10 | this.filename = null; 11 | this.folder = null; 12 | this.command = "raspistill"; 13 | this.parameters= []; 14 | 15 | this.takePicture = function takePicture(file,callback){ 16 | 17 | if (typeof(file) == "function") { 18 | callback = file; 19 | file=null; 20 | } 21 | 22 | if(!this.folder){ 23 | this.folder = util.format("%s/pitures", __dirname); 24 | } 25 | 26 | if(file){ 27 | this.filename = util.format("%s/%s", this.folder, file); 28 | }else{ 29 | this.filename = util.format("%s/%s.jpg", this.folder, new Date().toJSON()); 30 | } 31 | 32 | this.output(this.filename); 33 | 34 | this.command = "raspistill"; 35 | 36 | for (key in this.parameters) { 37 | 38 | //JD: remove this as .nopreview required a value in order to be set but the command did not require a value 39 | //if (this.parameters[key]){ 40 | this.command+= util.format(' %s %s ', key, this.parameters[key]); 41 | //} 42 | 43 | } 44 | 45 | var exec = require('child_process').exec,child; 46 | 47 | var self = this; 48 | 49 | console.log('executing...'); 50 | console.log(this.command); 51 | console.log('---'); 52 | 53 | child = exec(this.command,function (error, stdout, stderr) { 54 | 55 | if(callback!==undefined){ 56 | callback(self.filename,stderr); 57 | } 58 | 59 | }); 60 | }, 61 | this.recordVideo = function(file, callback){ 62 | 63 | if (typeof(file) == "function") { 64 | callback = file; 65 | file=null; 66 | } 67 | 68 | if(!this.folder){ 69 | this.folder = util.format("%s/videos", __dirname); 70 | } 71 | 72 | if(file){ 73 | this.filename = util.format("%s/%s", this.folder, file); 74 | }else{ 75 | this.filename = util.format("%s/%s.h264", this.folder, new Date().toJSON()); 76 | } 77 | 78 | this.output(this.filename); 79 | 80 | this.command = "raspivid"; 81 | 82 | 83 | //if we are streaming remove output command 84 | if(this.parameters['-o - >']){ 85 | delete this.parameters["-o"]; 86 | } 87 | 88 | for(key in this.parameters){ 89 | 90 | if(this.parameters[key]){ 91 | this.command+= util.format(' %s %s ', key, this.parameters[key]); 92 | } 93 | 94 | } 95 | 96 | var exec = require('child_process').exec,child; 97 | 98 | var self = this; 99 | 100 | child = exec(this.command,function (error, stdout, stderr) { 101 | 102 | if(callback!==undefined){ 103 | callback(self.filename,stderr); 104 | } 105 | 106 | }); 107 | 108 | }, 109 | this.prepare = function(parameters){ 110 | 111 | for(key in parameters){ 112 | this[key](parameters[key]); 113 | } 114 | 115 | return this; 116 | 117 | }, 118 | this.quality = function(value){ 119 | 120 | this.parameters['-q']= value; 121 | 122 | return this; 123 | }, 124 | this.width = function(value){ 125 | 126 | this.parameters['-w']= value; 127 | 128 | return this; 129 | }, 130 | this.height = function(value){ 131 | 132 | this.parameters['-h']= value; 133 | 134 | return this; 135 | }, 136 | this.preview = function(value){ 137 | 138 | this.parameters["-p"] = value; 139 | 140 | }, 141 | this.fullscreen = function(value){ 142 | 143 | if(value===undefined) 144 | value=""; 145 | 146 | this.parameters["-f"] = value; 147 | 148 | return this; 149 | 150 | }, 151 | this.nopreview = function(value) { 152 | 153 | if(value===undefined) 154 | value=""; 155 | 156 | this.parameters["-n"] = value; 157 | 158 | return this; 159 | 160 | }, 161 | this.opacity = function(value){ 162 | 163 | this.parameters["-op"] = value; 164 | 165 | return this; 166 | 167 | }, 168 | this.sharpness= function(value){ 169 | 170 | this.parameters["-sh"] = value; 171 | 172 | return this; 173 | 174 | }, 175 | this.contrast= function(value){ 176 | 177 | this.parameters["-co"] = value; 178 | 179 | return this; 180 | 181 | }, 182 | this.brightness= function(value){ 183 | 184 | this.parameters["-br"] = value; 185 | 186 | return this; 187 | 188 | }, 189 | this.saturation= function(value){ 190 | 191 | this.parameters["-sa"] = value; 192 | 193 | return this; 194 | 195 | }, 196 | this.ISO= function(value){ 197 | 198 | if(value===undefined) 199 | value=""; 200 | 201 | this.parameters["-ISO"] = value; 202 | 203 | return this; 204 | 205 | }, 206 | this.vstab= function(value){ 207 | 208 | if(value===undefined) 209 | value=""; 210 | 211 | this.parameters["-vs"] = value; 212 | 213 | return this; 214 | 215 | }, 216 | this.ev= function(value){ 217 | 218 | this.parameters["-ev"] = value; 219 | 220 | return this; 221 | 222 | }, 223 | this.exposure= function(value){ 224 | 225 | this.parameters["-ex"] = value; 226 | 227 | return this; 228 | 229 | }, 230 | this.awb= function(value){ 231 | 232 | this.parameters["-awb"] = value; 233 | 234 | return this; 235 | 236 | }, 237 | this.imxfx= function(value){ 238 | 239 | this.parameters["-ifx"] = value; 240 | 241 | return this; 242 | 243 | }, 244 | this.colfx= function(value){ 245 | 246 | this.parameters["-cfx"] = value; 247 | 248 | return this; 249 | 250 | }, 251 | this.metering= function(value){ 252 | 253 | if(value===undefined) 254 | value=""; 255 | 256 | this.parameters["-mm"] = value; 257 | 258 | return this; 259 | 260 | }, 261 | this.rotation= function(value){ 262 | 263 | this.parameters["-rot"] = value; 264 | 265 | return this; 266 | 267 | }, 268 | this.hflip= function(value){ 269 | 270 | if(value===undefined) 271 | value=""; 272 | 273 | this.parameters["-hf"] = value; 274 | 275 | return this; 276 | 277 | }, 278 | this.vflip= function(value){ 279 | 280 | if(value===undefined) 281 | value=""; 282 | 283 | this.parameters["-vf"] = value; 284 | 285 | return this; 286 | 287 | }, 288 | this.roi= function(value){ 289 | 290 | this.parameters["-roi"] = value; 291 | 292 | return this; 293 | 294 | }, 295 | this.shutter= function(value){ 296 | 297 | this.parameters["-s"] = value; 298 | 299 | return this; 300 | 301 | }, 302 | this.drc= function(value){ 303 | 304 | this.parameters["-drc"] = value; 305 | 306 | return this; 307 | 308 | }, 309 | this.raw= function(value){ 310 | 311 | if(value===undefined) 312 | value=""; 313 | 314 | this.parameters["-r"] = value; 315 | 316 | return this; 317 | 318 | }, 319 | this.output= function(value){ 320 | 321 | this.filename = value; 322 | this.parameters["-o"] = value; 323 | 324 | return this; 325 | 326 | }, 327 | this.latest= function(value){ 328 | 329 | this.parameters["-l"] = value; 330 | 331 | return this; 332 | 333 | }, 334 | this.verbose= function(value){ 335 | 336 | this.parameters["-v"] = value; 337 | 338 | return this; 339 | 340 | }, 341 | this.timeout= function(value){ 342 | 343 | this.parameters["-t"] = value; 344 | 345 | return this; 346 | 347 | }, 348 | this.timelapse= function(value){ 349 | 350 | this.parameters["-tl"] = value; 351 | 352 | return this; 353 | 354 | }, 355 | this.thumb= function(value){ 356 | 357 | this.parameters["-th"] = value; 358 | 359 | return this; 360 | 361 | }, 362 | this.demo= function(value){ 363 | 364 | this.parameters["-d"] = value; 365 | 366 | return this; 367 | 368 | }, 369 | this.encoding= function(value){ 370 | 371 | this.parameters["-e"] = value; 372 | 373 | return this; 374 | 375 | }, 376 | this.exif= function(value){ 377 | 378 | this.parameters["-x"] = value; 379 | 380 | return this; 381 | 382 | }, 383 | this.fullpreview= function(value){ 384 | 385 | if(value===undefined) 386 | value=""; 387 | 388 | this.parameters["-fp"] = value; 389 | 390 | return this; 391 | 392 | }, 393 | this.signal= function(value){ 394 | 395 | this.parameters["-s"] = value; 396 | 397 | return this; 398 | 399 | }, 400 | this.bitrate = function(value){ 401 | 402 | this.parameters["-b"] = value; 403 | 404 | return this; 405 | 406 | }, 407 | this.framerate = function(value){ 408 | 409 | this.parameters["-fps"] = value; 410 | 411 | return this; 412 | 413 | }, 414 | this.penc = function(value){ 415 | 416 | if(value===undefined) 417 | value=""; 418 | 419 | this.parameters["-e"] = value; 420 | 421 | return this; 422 | 423 | }, 424 | this.intra = function(value){ 425 | 426 | this.parameters["-g"] = value; 427 | 428 | return this; 429 | 430 | }, 431 | this.qp = function(value){ 432 | 433 | this.parameters["-qp"] = value; 434 | 435 | return this; 436 | 437 | }, 438 | this.profile = function(value){ 439 | 440 | this.parameters["-pf"] = value; 441 | 442 | return this; 443 | 444 | }, 445 | this.inline = function(value){ 446 | 447 | this.parameters["-ih"] = value; 448 | 449 | return this; 450 | 451 | }, 452 | this.timed = function(value){ 453 | 454 | this.parameters["-td"] = value; 455 | 456 | return this; 457 | 458 | }, 459 | this.initial = function(value){ 460 | 461 | this.parameters["-i"] = value; 462 | 463 | return this; 464 | 465 | }, 466 | this.segment = function(value){ 467 | 468 | this.parameters["-sg"] = value; 469 | 470 | return this; 471 | 472 | }, 473 | this.wrap = function(value){ 474 | 475 | this.parameters["-wr"] = value; 476 | 477 | return this; 478 | 479 | }, 480 | this.start = function(value){ 481 | 482 | this.parameters["-sn"] = value; 483 | 484 | return this; 485 | 486 | }, 487 | this.streamVideo = function(value){ 488 | 489 | this.parameters["-o - >"] = value; 490 | 491 | return this; 492 | 493 | } 494 | this.baseFolder = function(directory){ 495 | this.folder = directory; 496 | 497 | // JD added return this to allow chaining 498 | return this; 499 | }, 500 | this.reset = function(){ 501 | this.parameters= []; 502 | } 503 | }; 504 | 505 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "raspberry-pi-mjpeg-server", 3 | "version": "1.0.4", 4 | "description": "Node.js mjpeg streaming server for the Raspberry PI camera module", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node index.js" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/john-doherty/raspberry-pi-mjpeg-server.git" 13 | }, 14 | "homepage": "https://github.com/john-doherty/raspberry-pi-mjpeg-server#readme", 15 | "bugs": { 16 | "url": "https://github.com/john-doherty/raspberry-pi-mjpeg-server/issues" 17 | }, 18 | "author": "John Doherty (www.johndoherty.info)", 19 | "license": "ISC", 20 | "keywords": [ 21 | "raspberry", 22 | "pi", 23 | "camera", 24 | "rpi", 25 | "mjpeg", 26 | "server", 27 | "stream", 28 | "motion", 29 | "jpeg" 30 | ], 31 | "dependencies": { 32 | "chokidar": "^1.6.0", 33 | "commander": "^2.9.0", 34 | "ip":"^1.1.4", 35 | "pubsub-js": "~1.5.2" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /raspberry-pi-mjpeg-server.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var fs = require("fs"), 4 | os = require('os'), 5 | path = require('path'), 6 | http = require("http"), 7 | util = require("util"), 8 | chokidar = require('chokidar'), 9 | PubSub = require("pubsub-js"), 10 | localIp = require('ip'), 11 | PiCamera = require('./camera.js'), 12 | program = require('commander'), 13 | pjson = require('./package.json'); 14 | 15 | program 16 | .version(pjson.version) 17 | .description(pjson.description) 18 | .option('-p --port ', 'port number (default 8080)', parseInt) 19 | .option('-w --width ', 'image width (default 640)', parseInt) 20 | .option('-l --height ', 'image height (default 480)', parseInt) 21 | .option('-q --quality ', 'jpeg image quality from 0 to 100 (default 85)', parseInt) 22 | .option('-s --sharpness ', 'Set image sharpness (-100 - 100)', parseInt) 23 | .option('-c --contrast ', 'Set image contrast (-100 - 100)', parseInt) 24 | .option('-b --brightness ', 'Set image brightness (0 - 100) 0 is black, 100 is white', parseInt) 25 | .option('-s --saturation ', 'Set image saturation (-100 - 100)', parseInt) 26 | .option('-t --timeout ', 'timeout in milliseconds between frames (default 500)', parseInt) 27 | .option('-v --version', 'show version') 28 | .parse(process.argv); 29 | 30 | program.on('--help', function(){ 31 | console.log("Usage: " + pjson.name + " [OPTION]\n"); 32 | }); 33 | 34 | var port = program.port || 8080, 35 | width = program.width || 640, 36 | height = program.height || 480, 37 | timeout = program.timeout || 250, 38 | quality = program.quality || 75, 39 | sharpness = program.sharpness || 0, 40 | contrast = program.contrast || 0, 41 | brightness = program.brightness || 50, 42 | saturation = program.saturation || 0, 43 | tmpFolder = os.tmpdir(), 44 | tmpImage = pjson.name + '-image.jpg', 45 | localIpAddress = localIp.address(), 46 | boundaryID = "BOUNDARY"; 47 | 48 | /** 49 | * create a server to serve out the motion jpeg images 50 | */ 51 | var server = http.createServer(function(req, res) { 52 | 53 | // return a html page if the user accesses the server directly 54 | if (req.url === "/") { 55 | res.writeHead(200, { "content-type": "text/html;charset=utf-8" }); 56 | res.write(''); 57 | res.write(''); 58 | res.write('' + pjson.name + ''); 59 | res.write(''); 60 | res.write(''); 61 | res.write(''); 62 | res.write(''); 63 | res.end(); 64 | return; 65 | } 66 | 67 | if (req.url === "/healthcheck") { 68 | res.statusCode = 200; 69 | res.end(); 70 | return; 71 | }; 72 | 73 | // for image requests, return a HTTP multipart document (stream) 74 | if (req.url.match(/^\/.+\.jpg$/)) { 75 | 76 | res.writeHead(200, { 77 | 'Content-Type': 'multipart/x-mixed-replace;boundary="' + boundaryID + '"', 78 | 'Connection': 'keep-alive', 79 | 'Expires': 'Fri, 27 May 1977 00:00:00 GMT', 80 | 'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate', 81 | 'Pragma': 'no-cache' 82 | }); 83 | 84 | // 85 | // send new frame to client 86 | // 87 | var subscriber_token = PubSub.subscribe('MJPEG', function(msg, data) { 88 | 89 | //console.log('sending image'); 90 | 91 | res.write('--' + boundaryID + '\r\n') 92 | res.write('Content-Type: image/jpeg\r\n'); 93 | res.write('Content-Length: ' + data.length + '\r\n'); 94 | res.write("\r\n"); 95 | res.write(Buffer(data), 'binary'); 96 | res.write("\r\n"); 97 | }); 98 | 99 | // 100 | // connection is closed when the browser terminates the request 101 | // 102 | res.on('close', function() { 103 | console.log("Connection closed!"); 104 | PubSub.unsubscribe(subscriber_token); 105 | res.end(); 106 | }); 107 | } 108 | }); 109 | 110 | server.on('error', function(e) { 111 | if (e.code == 'EADDRINUSE') { 112 | console.log('port already in use'); 113 | } else if (e.code == "EACCES") { 114 | console.log("Illegal port"); 115 | } else { 116 | console.log("Unknown error"); 117 | } 118 | process.exit(1); 119 | }); 120 | 121 | // start the server 122 | server.listen(port); 123 | console.log(pjson.name + " started on port " + port); 124 | console.log('Visit http://' + localIpAddress + ':' + port + ' to view your PI camera stream'); 125 | console.log(''); 126 | 127 | 128 | var tmpFile = path.resolve(path.join(tmpFolder, tmpImage)); 129 | 130 | // start watching the temp image for changes 131 | var watcher = chokidar.watch(tmpFile, { 132 | persistent: true, 133 | usePolling: true, 134 | interval: 10, 135 | }); 136 | 137 | // hook file change events and send the modified image to the browser 138 | watcher.on('change', function(file) { 139 | 140 | //console.log('change >>> ', file); 141 | 142 | fs.readFile(file, function(err, imageData) { 143 | if (!err) { 144 | PubSub.publish('MJPEG', imageData); 145 | } 146 | else { 147 | console.log(err); 148 | } 149 | }); 150 | }); 151 | 152 | // setup the camera 153 | var camera = new PiCamera(); 154 | 155 | // start image capture 156 | camera 157 | .nopreview() 158 | .baseFolder(tmpFolder) 159 | .thumb('0:0:0') // dont include thumbnail version 160 | .timeout(9999999) // never end 161 | .timelapse(timeout) // how often we should capture an image 162 | .width(width) 163 | .height(height) 164 | .quality(quality) 165 | .sharpness(sharpness) 166 | .contrast(contrast) 167 | .brightness(brightness) 168 | .saturation(saturation) 169 | .takePicture(tmpImage); 170 | --------------------------------------------------------------------------------