diff options
Diffstat (limited to 'node-mjpeg-proxy.js')
-rw-r--r-- | node-mjpeg-proxy.js | 142 |
1 files changed, 39 insertions, 103 deletions
diff --git a/node-mjpeg-proxy.js b/node-mjpeg-proxy.js index 84f6a07..a0365d9 100644 --- a/node-mjpeg-proxy.js +++ b/node-mjpeg-proxy.js @@ -1,114 +1,50 @@ /************************** TODO: -1. better passing of the headers +x. better passing of the headers 2. Make in modular 3. Write it modular so it can be dynamically set for cam creds **************************/ -var http = require('http'); -var sys = require('sys'); -// port of the cam -var cam = http.createClient(80, '192.168.1.186'); -var allHttpClient = []; -// Starting the stream on the cam -var request = cam.request('GET', '/axis-cgi/mjpg/video.cgi?fps=5', - {'host': '192.168.1.186', - 'Authorization': "Basic ZnFubWFveGJtb3A6MTAyMzc3"}); -request.end(); -request.on('response', function (response) { - response.setEncoding('binary'); - response.on('data', function (chunk) { - - for(i=0; i < allHttpClient.length; i++) - { - allHttpClient[i].write(chunk, 'binary'); - } - - }); - -}); -var testServer = http.createServer(); +var http = require('http'), +sys = require('sys'), +url = require('url'); -testServer.on('request', function (req, res){ - res.writeHead(200, {"cache-control":"no-cache","pragma":"no-cache","expires":"Thu, 01 Dec 1994 16:00:00 GMT","content-type":"multipart/x-mixed-replace; boundary=myboundary"}); - allHttpClient.push(res); -}); +var feedurl = "http://192.168.0.196:8080/videofeed"; +feedurl = url.parse(feedurl); +// port of the cam +var cam = http.createClient(feedurl.port, feedurl.hostname); +var audienceClients = []; + +var testServer = http.createServer(); testServer.listen(5080, "0.0.0.0"); sys.puts('Server started on port 5080'); - - - - - - - - - - - - - - - - - - - - - - - - - - -/************************* -DEPRECATED SHIT -*************************/ -/******** READ AN IMAGE AND SEND IT ON CONNECTION *********/ -//var image = fs.readFileSync('/home/prene/Desktop/hotmail.png', 'binary', function(err, data){ -// sys.puts("yep gone through"); -// if(err) throw err; -// -// return data; -// }); - -//var server = http.createServer(function (req, res) { - -// res.writeHead(200, { "Content-Type": "text/jpg" -// , "Content-Length": image.length -// }); -// -// res.end(image, 'binary'); - -// -//}).listen(7878, "0.0.0.0"); - -/**** // *******/ - - -/************ STORE ALL SOCKETS CONNECTING *****************/ - -//sockets = []; -// - -//net.createServer(function (socket) { - -// socket.setEncoding("utf8"); -// socket.addListener("connect", function () { -// console.log("Connected"); -// -// }); - -// socket.addListener("end", function () { -// socket.end(); -// -// }); - -// sockets.push(socket); - -//}).listen(port, "0.0.0.0"); -//sys.puts("Server TCP listening on 127.0.0.1 on port: " + port); - -/************ // ********/ +// Starting the stream on the cam +var request = cam.request('GET', feedurl.pathname + + (feedurl.search ? feedurl.search : ""), + {'host': feedurl.hostname}); +request.end(); +request.on('response', function (srcResponse) { + + /** Setup server listener **/ + testServer.on('request', function (req, res) { + /** Replicate the header from the source **/ + res.writeHead(200, srcResponse.headers); + /** Push the client into the client list **/ + audienceClients.push(res); + /** Clean up connections when they're dead **/ + res.socket.on('close', function () { + audienceClients.splice(audienceClients.indexOf(res), 1); + }); + }); + + /** Send data to relevant clients **/ + srcResponse.setEncoding('binary'); + srcResponse.on('data', function (chunk) { + var i; + for (i = audienceClients.length; i--;) { + audienceClients[i].write(chunk, 'binary'); + } + }); +}); |