forked from phoboslab/jsmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream-server.js
145 lines (121 loc) · 4.28 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
if (process.argv.length < 3) {
console.log(
'Usage: \n' +
'node stream-server.js <secret> [<stream-port> <websocket-port>]'
);
process.exit();
}
var STREAM_SECRET = process.argv[2],
STREAM_PORT = process.argv[3] || 8082,
WEBSOCKET_PORT = process.argv[4] || 8084,
STREAM_MAGIC_BYTES = 'jsmp'; // Must be 4 bytes
var width = 320,
height = 240;
// Websocket Server
var socketServer = new (require('ws').Server)({
port: WEBSOCKET_PORT
});
socketServer.on('connection', function(socket) {
// Send magic bytes and video size to the newly connected socket
// struct { char magic[4]; unsigned short width, height;}
var streamHeader = new Buffer(8);
streamHeader.write(STREAM_MAGIC_BYTES);
streamHeader.writeUInt16BE(width, 4);
streamHeader.writeUInt16BE(height, 6);
socket.send(streamHeader, {
binary: true
});
console.log('New WebSocket Connection (' + socketServer.clients.length + ' total)');
socket.on('close', function(code, message) {
console.log('Disconnected WebSocket (' + socketServer.clients.length + ' total)');
});
});
socketServer.broadcast = function(data, opts) {
for (var i in this.clients) {
if (this.clients[i].readyState == 1) {
this.clients[i].send(data, opts);
} else {
console.log('Error: Client (' + i + ') not connected.');
}
}
};
// HTTP Server to accept incomming MPEG Stream
var streamServer = require('http').createServer(function(request, response) {
var params = request.url.substr(1).split('/');
if (params[0] == STREAM_SECRET) {
width = (params[1] || 320) | 0;
height = (params[2] || 240) | 0;
console.log(
'Stream Connected: ' + request.socket.remoteAddress +
':' + request.socket.remotePort + ' size: ' + width + 'x' + height
);
request.on('data', function(data) {
socketServer.broadcast(data, {
binary: true
});
});
} else {
console.log(
'Failed Stream Connection: ' + request.socket.remoteAddress +
request.socket.remotePort + ' - wrong secret.'
);
response.end();
}
}).listen(STREAM_PORT);
console.log('Listening for MPEG Stream on http://127.0.0.1:' + STREAM_PORT + '/<secret>/<width>/<height>');
console.log('Awaiting WebSocket connections on ws://127.0.0.1:' + WEBSOCKET_PORT + '/');
express = require('express');
request = require('request');
// var hue = require('hue-module');
// hue.discover(function(e) {
// console.log(e)
// });
// hue.load("192.168.178.19", "174fb5a039dabc8f3b1a51f02a78ec7f");
var hueUrl = "http://192.168.178.19/api/174fb5a039dabc8f3b1a51f02a78ec7f/lights/1/state";
app = express();
ws = require('websocket.io');
app.use(express["static"]('./'));
lightOn = true;
server = app.listen(5002);
console.log('started fileserver');
io = ws.attach(server);
io.on('connection', function(socket) {
return socket.on('message', function(data) {
var i, len, msg, ref, results, sock;
msg = JSON.parse(data);
switch (msg.type) {
case 'lights':
if (msg.data == 'on') {
console.log("Turn light on with bri=" + msg.bri);
request({
url: hueUrl,
method: 'PUT',
json: {
"on": true,
"bri": parseInt(msg.bri)
}
}, function(e) {
console.log(e);
})
// hue.light(1, function(light) {
// hue.change(light.set({
// "on": true,
// "bri": msg.data.bri
// }));
// });
} else if (msg.data == 'off') {
console.log("Turn light off");
request({
url: hueUrl,
method: 'PUT',
json: {
"on": false
}
}, function(e) {
console.log(e);
})
}
break;
}
});
});