forked from phoboslab/jsmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream-server.js
175 lines (147 loc) · 5.06 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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;
var portOffset = 0;
var mostWatched = {};
// Websocket Server
var socketServers = {}
function initialize_funcs(key) {
socketServers[key] = new (require('ws').Server)({port: WEBSOCKET_PORT+portOffset});
console.log("new socket server at port: "+(WEBSOCKET_PORT+portOffset));
socketServers[key].on('connection', function(socket) {
onConnection(this, socket);
});
socketServers[key].broadcast = function(data, opts) {
onBroadcast(this, data, opts)
};
mostWatched[key] = 0;
portOffset++;
}
function onConnection(thisServer, 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 ('+thisServer.clients.length+' total)' );
socket.on('close', function(code, message){
console.log( 'Disconnected WebSocket ('+thisServer.clients.length+' total)' );
});
}
function onBroadcast(thisServer, data, opts) {
for( var i in thisServer.clients ) {
if (thisServer.clients[i].readyState == 1) {
thisServer.clients[i].send(data, opts);
}
else {
console.log( 'Error: Client ('+i+') not connected.' );
}
}
}
//Express http server config
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var request = require('request');
var streamServer = require('http').createServer(app);
var util = require('util');
app.use(express.static(__dirname + '/app'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(function(request, response, next){
response.setHeader('Access-Control-Allow-Origin', '*');
next();
});
streamServer.listen(STREAM_PORT);
app.post("/"+STREAM_SECRET+"/:width?/:height?/:vid?", function(request, response){
response.connection.setTimeout(0);
width = (request.params.width || 320)|0;
height = (request.params.height || 240)|0;
console.log(request.method);
console.log(
'Stream Connected: ' + request.socket.remoteAddress +
':' + request.socket.remotePort + ' size: ' + width + 'x' + height
);
var vidServ = (typeof request.params.vid === 'undefined') ? "default" : request.params.vid;
console.log("capture "+vidServ);
request.on('data', function(data){
if (!(vidServ in socketServers)) {
initialize_funcs(vidServ)
}
socketServers[vidServ].broadcast(data, {binary:true});
});
});
app.get("/api/update-count/:id", function(request, response){
if (request.params.id in mostWatched)
mostWatched[request.params.id] += 1;
else
mostWatched[request.params.id] = 1;
return response.end();
});
app.get("/api/get-count", function(request, response){
var array=[];
for(a in mostWatched){
array.push([a,mostWatched[a]])
}
array.sort(function(a,b){return a[1] - b[1]});
array.reverse();
var arrayKeys = [];
for (a in array) {
console.log(array[a]);
arrayKeys.push(array[a][0]);
}
return response.end(JSON.stringify(arrayKeys));
});
// 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 ) {
// response.connection.setTimeout(0);
// width = (params[1] || 320)|0;
// height = (params[2] || 240)|0;
// console.log(request.method);
// console.log(
// 'Stream Connected: ' + request.socket.remoteAddress +
// ':' + request.socket.remotePort + ' size: ' + width + 'x' + height
// );
// var vidServ = (typeof params[3] === 'undefined') ? "default" : params[3];
// console.log("capture "+vidServ);
// request.on('data', function(data){
// if (!(vidServ in socketServers)) {
// initialize_funcs(vidServ)
// }
// socketServers[vidServ].broadcast(data, {binary:true});
// });
// } else {
// if (request.headers["x-requested-with"] != 'XMLHttpRequest') {
// console.log(
// 'Failed Stream Connection: '+ request.socket.remoteAddress +
// request.socket.remotePort + '.'
// );
// }
// console.log(
// 'New ajax request: '+ request.socket.remoteAddress +
// request.socket.remotePort
// );
// // response.writeHead(200, {'Content-Type': 'application/json'});
// // response.end(JSON.stringify({ 'a': 1 }));
// response.writeHead(200, {'Content-Type': 'text/plain'});
// response.end("formOutput");
// // response.end(formOutput);
// // response.end();
// }
// }).listen(STREAM_PORT);
console.log('Listening for MPEG Stream on http://127.0.0.1:'+STREAM_PORT+'/<secret>/<width>/<height>/<vid-name>');
console.log('Awaiting WebSocket connections on ws://127.0.0.1:'+WEBSOCKET_PORT+'/');