-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
80 lines (77 loc) · 2.41 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// include all the libraries we need as well as variables
require('bufferjs/add-chunk');
var http = require('http'),
io = require('socket.io').listen(
8080, {
log: false
}
), // our Socket.io websocket
options = {
host: "10.0.0.1",
port: 60152,
path: '/liveview.JPG?%211234%21http%2dget%3a%2a%3aimage%2fjpeg%3a%2a%21%21%21%21%21'
}, // options for our Sony liveview
parts, // image chunks buffer
bufferArray = [];
var _gsock; // global socket holder
var processing = false; // flag for processing the queue
var httpReq;
// on connection, stream the video
io.sockets.on('connection', function (socket) {
_gsock = socket; // set our global socket
handleHttp();
// verify delivery to drop frames if necessary
socket.on('done', function (data) {
// do something
});
socket.on('disconnect', function () {
console.log('Got disconnect!');
});
});
function handleHttp(){
// open a stream to the camera
httpReq = http.get(options, function (res) {
res.on('error', function (err) {
console.log("RESPONSE ERROR FROM CAMERA");
console.log(err);
});
// handle the data
res.on('data', function (buf) {
// simply add the buffer to our queue
bufferArray.push(buf);
io.sockets.emit("bufferpush", true);
handleQueue(buf);
//io.sockets.emit("raw", buf);
});
});
}
var frameCount = 0;
var jpgSize = 0;
var handleQueue = function () {
processing = true;
httpReq.end();
var buf = bufferArray.shift();
if (buf !== undefined) {
if (buf[0] == 0xff && buf[1] == 0x01) {
if (parts !== null && parts !== undefined) {
if(parts.length === jpgSize) {
var base64Image = parts.toString('base64');
io.sockets.emit('image', base64Image);
}
} /* END if */
var jpgSizeBuf = buf.slice(12, 15);
jpgSize = byteArrayToLong(jpgSizeBuf);
parts = new Buffer(jpgSize);
} else {
parts.addChunk(buf);
} /* END if */
}
};
// shift the bytes in the array
var byteArrayToLong = function ( /*byte[]*/ byteArray) {
var value = 0;
for (var i = byteArray.length - 1; i >= 0; i--) {
value += (byteArray[i] << 8) | (byteArray[i] & 0xff);
}
return value;
};