Skip to content

Commit

Permalink
0.0.4
Browse files Browse the repository at this point in the history
Fix ws stream.
Adaptive for use as linux service.
Create Farm class for many records.
  • Loading branch information
shoom3301 committed Dec 4, 2015
1 parent b9640af commit fe5142b
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 34 deletions.
52 changes: 38 additions & 14 deletions RTSPRecorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@
/**
* Rtsp stream recorder and streamer
* @param params {object} parameters
* @param name {string} name if recorder
* @constructor
*/
var Recorder = function(params){
var Recorder = function(params, name){
this.name = name || '';
//url to stream
this.url = '';
//stream for frite video to file
Expand Down Expand Up @@ -144,6 +146,7 @@

this.readStream.stdout.on('close', function() {
self._readStarted = false;
self.readStream = null;
self.reconnect();
});

Expand All @@ -166,17 +169,27 @@
}else{
this.emit('lostConnection');
this.log('Connection lost \r\n');

process.exit(1);
}

return this;
};

/**
* Path to records folder
* @returns {string}
*/
this.recordsPath = function(){
return this.folder+(this.name?(this.name+'/'):'');
};

/**
* Record stream to file
*/
this.recordStream = function(){
this.clearDir(function(){
var filename = this.folder+this.prefix+dateString()+'.mp4';
var filename = this.recordsPath()+this.prefix+dateString()+'.mp4';
this.writeStream = fs.createWriteStream(filename);
this.readStream.stdout.pipe(this.writeStream);

Expand All @@ -186,7 +199,7 @@
self.writeStream.end();
}, this.timeLimit*1000);

this.log("Start record "+filename+"\r\n");
this.log("Start record "+filename);
});

return this;
Expand Down Expand Up @@ -248,30 +261,32 @@
*/
this.wsStream = function(port, cb){
function start(){
this.wsServer = new ws.Server({
self.wsServer = new ws.Server({
port: port
});

this.wsServer.on("connection", function(socket) {
self.wsServer.on("connection", function(socket) {
var streamHeader = new Buffer(8);
streamHeader.write(STREAM_MAGIC_BYTES);
streamHeader.writeUInt16BE(self.movieWidth, 4);
streamHeader.writeUInt16BE(self.movieHeight, 6);
socket.send(streamHeader, {binary:true});
});

this.wsServer.broadcast = function(data, opts) {
self.wsServer.broadcast = function(data, opts) {
var i, _results;
_results = [];
for (i in this.clients) {
if (this.clients[i].readyState === 1) {
_results.push(this.clients[i].send(data, opts));
var clients = self.wsServer.clients;

for (i in clients) {
if (clients[i].readyState === 1) {
_results.push(clients[i].send(data, opts));
}
}
return _results;
};

this.on('camData', function(data){
self.on('camData', function(data){
return self.wsServer.broadcast(data);
});

Expand All @@ -280,13 +295,13 @@
if(cb) cb();
}

if(this.movieWidth){
start.apply(this);
if(self.movieWidth){
start.apply(self);
}else{
this.once('haveMovieSize', start);
self.once('haveMovieSize', start);
}

return this;
return self;
};

/**
Expand All @@ -295,6 +310,15 @@
* @see recordStream
*/
this.initialize = function(){
//Create records directory if not exist
try{
if(!fs.lstatSync(this.recordsPath()).isDirectory()){
fs.mkdirSync(this.recordsPath());
}
}catch (e){
fs.mkdirSync(this.recordsPath());
}

this.on('readStart', function(){
self.maxTryReconnect = 5;
self.recordStream();
Expand Down
76 changes: 76 additions & 0 deletions RecorderFarm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Created by Shoom on 04.12.15.
*/

(function(){
var child_process = require('child_process');

/**
* Farm of recorders
* @param childFile {string} path to file with child process start
* @param cameras {object} array of cameras configs
* @constructor
*/
var Farm = function(childFile, cameras){
var self = this;
this.collection = {};

/**
* Spawn child proccess with recorder
* @param conf {object} recorder configuration
* @param name {string} name of recorder
* @param restart {boolean} is restart spawn
*/
this.spawnChild = function(conf, name, restart){
var old = this.collection[name];
if(old){
if(old.connected){
old.kill();
old.disconnect();
}
delete self.collection[name];
}

var child = child_process.spawn("node",
[childFile, name, JSON.stringify(conf)],
{detached: false}
);

child.stdout.on('data', function (data) {
console.log(name+': ' + data);
});

child.stderr.on('data', function (data) {
console.log(name+' ERROR: ' + data);
});

child.on('close', function (code) {
console.log(name+' closed: ' + code);
//Restart proccess when him close
self.spawnChild(conf, name, 1);
});

if(restart){
console.log(name+ ' REstarted!');
}else{
console.log(name+ ' started!');
}

self.collection[name] = child;
};

/**
* Initialization
*/
this.initialize = function(){
//run
for(var _name in cameras){
if(cameras.hasOwnProperty(_name)){
this.spawnChild(cameras[_name], _name);
}
}
};
};

module.exports = Farm;
})();
24 changes: 24 additions & 0 deletions example/cameras.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"front": {
"url": "rtsp://admin:[email protected]/11",
"timeLimit": 60,
"folder": "videos/",
"prefix": "f",
"movieWidth": 1280,
"movieHeight": 720,
"maxDirSize": 20480,
"maxTryReconnect": 15,
"wsPort": 8001
},
"back": {
"url": "rtsp://admin:[email protected]/11",
"timeLimit": 60,
"folder": "videos/",
"prefix": "b",
"movieWidth": 1280,
"movieHeight": 720,
"maxDirSize": 20480,
"maxTryReconnect": 15,
"wsPort": 8002
}
}
37 changes: 25 additions & 12 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@
* Created by Shoom on 02.12.15.
*/

var Recorder = require('./../RTSPRecorder');
var oneCamera = false;

var rec = new Recorder({
url: 'rtsp://login:[email protected]/path',
timeLimit: 10,
folder: 'videos/',
prefix: 'vid-',
movieWidth: 1280,
movieHeight: 720,
maxDirSize: 1024*20,
maxTryReconnect: 15
if(oneCamera){
var Recorder = require('./../RTSPRecorder');

});
var rec = new Recorder({
url: 'rtsp://admin:[email protected]/11',
timeLimit: 10,
folder: 'videos/',
prefix: 'vid-',
movieWidth: 1280,
movieHeight: 720,
maxDirSize: 1024*20,
maxTryReconnect: 15

rec.initialize().wsStream(8001);
});

rec.initialize().wsStream(8001);
}else{
var RecorderFarm = require('./../RecorderFarm');

var farm = new RecorderFarm(
'example/runCamera.js',
require('./cameras.json')
);

farm.initialize();
}
25 changes: 18 additions & 7 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@
<script type="application/javascript" src="jsmpg.js"></script>
</head>
<body>
<canvas id="videoCanvas" width="500" height="500"></canvas>
<canvas id="videoCanvas1" width="500" height="500"></canvas>
<canvas id="videoCanvas2" width="500" height="500"></canvas>
<script>
var canvas = document.getElementById('videoCanvas');
client = new WebSocket('ws://192.168.1.137:8001');
player = new jsmpeg(client, {
canvas: canvas

var oneCamera = false;

var player1 = new jsmpeg( new WebSocket('ws://localhost:8001'), {
canvas: document.getElementById('videoCanvas1')
});
player.play();

player1.play();

if(!oneCamera){
var player2 = new jsmpeg( new WebSocket('ws://localhost:8002'), {
canvas: document.getElementById('videoCanvas2')
});

player2.play();
}
</script>
</body>
</html>gpartegpag
</html>
9 changes: 9 additions & 0 deletions example/runCamera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Created by Shoom on 04.12.15.
*/

var Recorder = require('./../RTSPRecorder');

var conf = JSON.parse(process.argv[3]);

new Recorder(conf, process.argv[2]).initialize().wsStream(conf.wsPort);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rtsp-recorder",
"version": "0.0.3",
"version": "0.0.4",
"description": "Record and stream RTSP",
"main": "RTSPRecorder.js",
"repository": {
Expand Down

0 comments on commit fe5142b

Please sign in to comment.