From 6f1f5f590828ef0c6714a66009a5be79f04361f7 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Sat, 16 Jul 2016 15:49:19 +0200 Subject: [PATCH] Prevent 'after' commands running before 'during' is done Added helper for Foscam URL's --- capture.js | 21 ++++++++++++-- config-foscam.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ config.sample.js | 13 +++++++-- 3 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 config-foscam.js diff --git a/capture.js b/capture.js index eb4f1c1..96b63bf 100644 --- a/capture.js +++ b/capture.js @@ -87,6 +87,8 @@ module.exports = var timer = null; var req = null; var output = new (require('./output-' + cam.output))(camId, cam.outputOptions, now); + var duringDone = false; + var queueAfter = false; runCommands(cam.before, 'before', function() @@ -105,13 +107,25 @@ module.exports = output = null; } - runCommands(cam.after, 'after', function() { }); + // If the stream is cut short, or the time is configured + // to be less than the duration of the 'during' commands, + // queue it up. + if (duringDone) + runCommands(cam.after, 'after', function() { }); + else + queueAfter = true; }; - req = http.request(cam.url, function(res) { - runCommands(cam.during, 'during', function() { }) + runCommands(cam.during, 'during', function() + { + if (queueAfter) + runCommands(cam.after, 'after', function() { }); + else + duringDone = true; + }); + timer = setTimeout(function() { @@ -119,6 +133,7 @@ module.exports = cleanup(); }, cam.time || config.defaultTime || 10000); + res.on('end', function() { cleanup(); diff --git a/config-foscam.js b/config-foscam.js new file mode 100644 index 0000000..2a9341c --- /dev/null +++ b/config-foscam.js @@ -0,0 +1,71 @@ +var url = require('url'); +var foscam = {}; + +// Based on: www.foscam.es/descarga/ipcam_cgi_sdk.pdf +// Tested on FI8910W + +function baseUrl(host, cgi, username, password) +{ + var urlObject = { + protocol: 'http:', + host: host, + pathname: cgi, + query: {} + }; + + if (typeof(username) !== 'undefined') + urlObject.query.user = username; + + if (typeof(password) !== 'undefined') + urlObject.query.pwd = password; + + return urlObject; +} + + +/* + rate: + image data translate speed, value range 0-23 + + 0: full speed + 1: 20 fps + 3: 15 fps + 6: 10 fps + 11: 5 fps + 12: 4 fps + 13: 3 fps + 14: 2 fps + 15: 1 fps + 17: 1 fp/2s + 19: 1 fp/3s + 21: 1 fp/4s + 23: 1 fp/5s + + resolution: + image resolution (8: 320*240, 32: 640*480) +*/ +foscam.mjpegStream = function(host, username, password, rate, resolution) +{ + var urlObject = baseUrl(host, 'videostream.cgi', username, password); + + if (typeof(resolution) !== 'undefined') + urlObject.query.resolution = resolution; + + if (typeof(rate) !== 'undefined') + urlObject.query.rate = rate; + + return url.format(urlObject); +} + +foscam.gotoPreset = function(host, presetNumber, username, password) +{ + if (presetNumber < 1) presetNumber = 1; + if (presetNumber > 32) presetNumber = 32; + + var urlObject = baseUrl(host, 'decoder_control.cgi', username, password); + urlObject.query.command = 29 + (presetNumber * 2); + + return url.format(urlObject); +} + +module.exports = foscam; \ No newline at end of file diff --git a/config.sample.js b/config.sample.js index cbb2712..771591e 100644 --- a/config.sample.js +++ b/config.sample.js @@ -1,3 +1,4 @@ +var foscam = require('./config-foscam'); var config = {}; config.port = 5705; @@ -7,7 +8,9 @@ config.cams = { frontdoor: { - url: 'http://10.138.1.10/videostream.cgi?user=viewer&pwd=verysecure', + // You can specify any URL string here, the Foscam helper just makes it easier + // for compatible models. If you add your own config helper, please publish! + url: foscam.mjpegStream('10.138.1.10', 'viewer', 'verysecure'), time: 5000, output: 'ffmpeg', @@ -18,11 +21,17 @@ config.cams = videoCodec: 'libx264', filename: '[/srv/www/publiccam/]YYYY-MM-DD HH.mm.ss[/.avi]' } + + // TODO examples for before/after/during commands }, backdoor: { - url: 'http://10.138.1.11/videostream.cgi?user=viewer&pwd=verysecure', + // You can use username:password@ in the URL to log in with basic + // authentication. Note that some cams, like Foscam, use Digest + // authentication which is not supported. For Foscam you can provide + // the login in the parameters instead. + url: 'http://viewer:verysecure@10.138.1.11/video.cgi', time: 5000, output: 'mjpeg-split',