-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_server.js
executable file
·89 lines (67 loc) · 2.15 KB
/
stream_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
81
82
83
84
85
86
87
88
89
const spawn = require('child_process').spawn;
const Splitter = require('stream-split');
const NALseparator = new Buffer([0,0,0,1]);
class Server {
constructor(wss, options) {
console.log('Creating the stream server...');
this.options = Object.assign({}, {
width : 960,
height: 540,
fps: 12
}, options);
this.wss = wss;
this.new_client = this.new_client.bind(this);
this.start_feed = this.start_feed.bind(this);
this.broadcast = this.broadcast.bind(this);
this.wss.on('connection', this.new_client);
}
start_feed() {
var readStream = this.get_feed();
this.readStream = readStream;
readStream = readStream.pipe(new Splitter(NALseparator));
readStream.on("data", this.broadcast);
}
get_feed() {
var msk = "raspivid -t 0 -o - -w %d -h %d -fps %d";
//var cmd = util.format(msk, this.options.width, this.options.height, this.options.fps);
// console.log(cmd);
var streamer = spawn('raspivid', ['-t', '0', '-o', '-', '-w', this.options.width, '-h', this.options.height, '-fps', this.options.fps, '-pf', 'baseline']);
streamer.on("exit", function(code){
console.log("Failure", code);
});
return streamer.stdout;
}
broadcast(data) {
this.wss.clients.forEach(function(socket) {
if(socket.buzy)
return;
socket.buzy = true;
socket.buzy = false;
socket.send(Buffer.concat([NALseparator, data]), { binary: true}, function ack(error) {
socket.buzy = false;
});
});
}
new_client(socket) {
var self = this;
console.log('New guy');
socket.send(JSON.stringify({
action : "init",
width : this.options.width,
height : this.options.height,
}));
socket.on("message", function(data){
var cmd = "" + data, action = data.split(' ')[0];
console.log("Incomming action '%s'", action);
if(action == "REQUESTSTREAM")
self.start_feed();
if(action == "STOPSTREAM")
self.readStream.pause();
});
socket.on('close', function() {
self.readStream.end();
console.log('stopping client interval');
});
}
};
module.exports = Server;