I have a working NodeJS application that uses UDP to stream camera data from a Kinect device to a browser.
It works fine on one Windows 8 machine, but on a 2nd install, the server launches without error, then in less than a second it always quits. This is my server code without the Kinect module dependencies.
The problem seems to lie in the Kinect specific code, specifically the Buffer and resizeBuffer. If I comment out these lines
resizedBuffer[j] = data[i];
resizedBuffer[j + 1] = data[i + 1];
resizedBuffer[j + 2] = data[i + 2];
resizedBuffer[j + 3] = data[i + 3];
The application runs, not fully functional, but it doesn't quit. Is it possible its memory related? Can any nodejs experts suggest any debugging steps I should take?
var express = require('express');
var Kinect2 = require('./kinect2');
var kinect = new Kinect2();
var app = express();
zlib = require('zlib');
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
app.use(express.static(__dirname + '/public'));
var dgram = require('dgram');
var dGramSocket = dgram.createSocket('udp4');
dGramSocket.bind(7788);
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
console.log('a user connected');
socket.on('my other event', function (data) {
console.log(data);
});
setTimeout(function () {
socket.emit('news', { msg: '2 second delay from socket server' });
}, 2000);
socket.emit('news', { msg: 'new user connected from socket server' });
});
dGramSocket.on('message', function (content, rinfo) {
console.log('got message from', rinfo.address, rinfo.port);
var dir = content.toString().trim();
var info=content;
if (/^r/.test(dir)) {
io.sockets.emit('right', { msg: 'lorem ipsum' });
}
if (/^l/.test(dir)) {
console.log('content.toString() AFTER TRIM');
io.sockets.emit('left', { msg: 'lorem ipsum' });
}
});
//---------------------------------------
// KINECT CODE START
var compression = 3;
var origWidth = 1920;
var origHeight = 1080;
var origLength = 4 * origWidth * origHeight;
var compressedWidth = origWidth / compression;
var compressedHeight = origHeight / compression;
var resizedLength = 4 * compressedWidth * compressedHeight;
var resizedBuffer = new Buffer(resizedLength);
var compressing = false;
if (kinect.open()) {
server.listen(8000);
var compressing = false;
kinect.on('colorFrame', function (data) {
//compress the depth data using zlib
if (!compressing) {
compressing = true;
var y2 = 0;
for (var y = 0; y < origHeight; y += compression) {
y2++;
var x2 = 0;
for (var x = 0; x < origWidth; x += compression) {
var i = 4 * (y * origWidth + x);
var j = 4 * (y2 * compressedWidth + x2);
resizedBuffer[j] = data[i];
resizedBuffer[j + 1] = data[i + 1];
resizedBuffer[j + 2] = data[i + 2];
resizedBuffer[j + 3] = data[i + 3];
x2++;
}
}
zlib.deflate(resizedBuffer, function (err, result) {
if (!err) {
io.sockets.emit('colorFrame', result.toString('base64'));
}
compressing = false;
});
}//end if compressing
});
kinect.openColorReader();
}