summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--index.js4
-rw-r--r--node-mjpeg-proxy.js71
2 files changed, 39 insertions, 36 deletions
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..5284ba9
--- /dev/null
+++ b/index.js
@@ -0,0 +1,4 @@
+exports.Proxy = require('./node-mjpeg-proxy').Proxy;
+exports.createProxy = function (srcURL, options) {
+ return new exports.Proxy(srcURL, options || {});
+};
diff --git a/node-mjpeg-proxy.js b/node-mjpeg-proxy.js
index a0365d9..831ef55 100644
--- a/node-mjpeg-proxy.js
+++ b/node-mjpeg-proxy.js
@@ -1,50 +1,49 @@
/**************************
TODO:
-x. better passing of the headers
-2. Make in modular
-3. Write it modular so it can be dynamically set for cam creds
+1. Make it work with existing HTTP servers and listen to resource URLs
**************************/
var http = require('http'),
sys = require('sys'),
url = require('url');
-var feedurl = "http://192.168.0.196:8080/videofeed";
-feedurl = url.parse(feedurl);
+Proxy = exports.Proxy = function (srcURL, options) {
+ if (!srcURL) throw new Error("Please provide a source feed URL");
+ srcURL = url.parse(srcURL);
-// port of the cam
-var cam = http.createClient(feedurl.port, feedurl.hostname);
-var audienceClients = [];
+ var srcClient = http.createClient(srcURL.port || 80, srcURL.hostname);
-var testServer = http.createServer();
-testServer.listen(5080, "0.0.0.0");
-sys.puts('Server started on port 5080');
+ var audienceServer = options.audienceServer || http.createServer();
+ var audienceServerPort = options.port || 5080;
+ var audienceClients = [];
-// 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);
+ // Starting the stream on from the source
+ var request = srcClient.request('GET', srcURL.pathname +
+ (srcURL.search ? srcURL.search : ""),
+ {'host': srcURL.hostname});
+ request.end();
+ request.on('response', function (srcResponse) {
+ /** Setup Audience server listener **/
+ audienceServer.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);
+ });
});
- });
+ audienceServer.listen(audienceServerPort);
+ sys.puts('node-mjpeg-proxy server started on port 5080');
- /** 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');
- }
+ /** 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');
+ }
+ });
});
-});
+};