-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
185 lines (151 loc) · 4.57 KB
/
index.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
176
177
178
179
180
181
182
183
184
185
var express = require('express')
, http = require('http')
, app = express()
, logger = require('log4js').getLogger()
, WebSocket = require('ws')
, conf = require('./config/config.json')
var port = process.env.PORT || 3000
, raspi_addr = process.env.RASPI_ADDR || conf.raspi.addr
, raspi_port = process.env.RASPI_PORT || conf.raspi.port
, raspi_path = process.env.RASPI_PATH || conf.raspi.path
, peerjs_scheme = process.env.PEERJS_SCHEME || conf.peerjs.scheme
, peerjs_addr = process.env.PEERJS_ADDR || conf.peerjs.addr
, peerjs_port = process.env.PEERJS_PORT || conf.peerjs.port
, peerjs_path = process.env.PEERJS_PATH || conf.peerjs.path
, peerjs_key = process.env.PEERJS_APIKEY || conf.peerjs.apikey
, origin = process.env.ORIGIN || conf.peerjs.origin4api
app.use(express.static(__dirname + '/public'));
var server = http.createServer(app);
server.listen(port, function(){
var host_ = server.address().address
, port_ = server.address().port
logger.info("raspi4peerjs listening at http://%s:%s", host_, port_);
});
///////////////////////////////////////////////////////////////////
// utility
var util = {};
util.randomToken = function() {
return Math.random().toString(36).substr(2);
}
util.randomId = function(){
return Math.floor(Math.random() * 10000);
}
///////////////////////////////////////////////////////////////////
// adapter for raspberry pi uv4l server
var raspiSocket = new WebSocket("ws://" + raspi_addr + ":" + raspi_port + raspi_path, {
"origin": origin
});
raspiSocket.on("open", function(){
logger.info("connection established for raspisocket");
});
raspiSocket.on("close", function(){
logger.info("closed connection to raspisocket");
});
raspiSocket.on("message", function(msg){
logger.info("message received from raspi", msg);
try{
// check whether peer connection is established or not.
if(!peer_id) {
logger.warn("receive message from raspi-cam, but there is no peer connection");
return;
}
var msg_ = JSON.parse(msg);
var type = null;
switch(msg_.type) {
case "offer":
type = "OFFER";
break;
case "geticecandidate":
type = "CANDIDATE";
break;
default:
}
if(type) {
var obj = {
"type": type,
"src": myid.toString(),
"dst": peer_id.toString(),
"payload": msg_
};
logger.info(obj);
peerjsSocket.send(JSON.stringify(obj));
} else {
logger.warn("received invalid message from raspi-cam");
}
} catch(e) {
logger.error(e);
}
});
///////////////////////////////////////////////////////////////////
// adapter for raspberry pi uv4l server
var myid = util.randomId()
, token = util.randomToken()
, peer_id = null;
var peerjs_url = [
peerjs_scheme,
peerjs_addr,
":",
peerjs_port,
peerjs_path,
"peerjs?key=" + peerjs_key,
"&id=" + myid,
"&token=" + token
].join("");
var peerjsSocket = new WebSocket(peerjs_url, { "origin": origin });
peerjsSocket.on("open", function(){
logger.info(peerjs_url);
logger.info("connection established for peerjs server(%s) [myid: %s, token: %s]", peerjs_addr, myid, token);
});
peerjsSocket.on("error", function(err) {
logger.warn(err);
});
peerjsSocket.on("close", function(){
logger.info("closed connection to peerjsSocket");
});
peerjsSocket.on("message", function(msg){
logger.info("message received from peerjs server", msg);
try{
var msg_ = JSON.parse(msg);
var data = null;
switch(msg_.type) {
case "OPEN":
return;
break;
case "OFFER":
peer_id = msg_.src;
data = msg_.payload;
break;
case "ANSWER":
case "CANDIDATE":
case "LEAVE":
if(!!peer_id || peer_id === msg_.src) {
data = msg_.payload;
}
break;
case "PING":
this.send(JSON.stringify({"type": "PONG"}));
break;
default:
break;
}
if(data) {
raspiSocket.send(JSON.stringify(data));
} else {
if(msg_.type !=="PING") logger.warn("received invalid message from peer server");
}
} catch(e) {
logger.error(e);
}
});
//////////////////////////////////////////////////////////////////////////
// observe SIGINT
process.stdin.resume();
process.on('SIGINT', function(){
// todo: check readystate === 1 or not
if(raspiSocket) raspiSocket.close();
if(peerjsSocket) peerjsSocket.close();
logger.info("process will going down in 2 seconds...");
setTimeout(function(ev){
process.exit();
}, 2000);
});