-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket-relay.js
146 lines (134 loc) · 3.98 KB
/
websocket-relay.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
/**
* Creation Date: March 8, 2020
* Author: Luke Slevinsky
* Taken from: https://github.com/phoboslab/jsmpeg/blob/master/websocket-relay.js
* Streaming relay to send stream to clients connected to the WebSocket
*/
// Use the websocket-relay to serve a raw MPEG-TS over WebSockets. You can use
// ffmpeg to feed the relay. ffmpeg -> websocket-relay -> browser
// Example:
// node websocket-relay yoursecret 8081 8082
// ffmpeg -i <some input> -f mpegts http://localhost:8081/yoursecret
const CONSTANTS = require("./constants");
const { STREAM_COMMAND, FFMPEG_ARGS } = CONSTANTS;
var fs = require("fs"),
http = require("http"),
https = require("https"),
WebSocket = require("ws"),
spawn = require("child_process").spawn;
var socketServer;
if (process.argv.length < 3) {
console.log(
"Usage: \n" +
"node websocket-relay.js <secret> [<stream-port> <websocket-port> -https]"
);
process.exit();
}
var STREAM_SECRET = process.argv[2],
STREAM_PORT = process.argv[3] || 8081,
WEBSOCKET_PORT = process.argv[4] || 8082,
USE_HTTPS = process.argv[5] === "true" || false,
RECORD_STREAM = false;
// Websocket Server
if (USE_HTTPS) {
const secureSocketServer = https
.createServer({
key: fs.readFileSync("./localhost.key"),
cert: fs.readFileSync("./localhost.crt")
})
.listen(WEBSOCKET_PORT);
socketServer = new WebSocket.Server({
server: secureSocketServer
});
} else {
socketServer = new WebSocket.Server({
port: WEBSOCKET_PORT,
perMessageDeflate: false
});
}
socketServer.connectionCount = 0;
socketServer.on("connection", (socket, upgradeReq) => {
socketServer.connectionCount++;
console.log(
"New WebSocket Connection: ",
(upgradeReq || socket.upgradeReq).socket.remoteAddress,
(upgradeReq || socket.upgradeReq).headers["user-agent"],
"(" + socketServer.connectionCount + " total)"
);
socket.on("close", (code, message) => {
socketServer.connectionCount--;
console.log(
"Disconnected WebSocket (" + socketServer.connectionCount + " total)"
);
});
socket.on("error", (code, message) => {
socketServer.connectionCount--;
console.log("error");
});
});
socketServer.broadcast = data => {
socketServer.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
};
// HTTP Server to accept incomming MPEG-TS Stream from ffmpeg
var streamServer = http
.createServer((request, response) => {
var params = request.url.substr(1).split("/");
if (params[0] !== STREAM_SECRET) {
console.log(
"Failed Stream Connection: " +
request.socket.remoteAddress +
":" +
request.socket.remotePort +
" - wrong secret."
);
response.end();
}
response.connection.setTimeout(0);
console.log(
"Stream Connected: " +
request.socket.remoteAddress +
":" +
request.socket.remotePort
);
request.on("data", data => {
socketServer.broadcast(data);
if (request.socket.recording) {
request.socket.recording.write(data);
}
});
request.on("end", () => {
console.log("close");
if (request.socket.recording) {
request.socket.recording.close();
}
});
// Record the stream to a local file?
if (RECORD_STREAM) {
var path = "recordings/" + Date.now() + ".ts";
request.socket.recording = fs.createWriteStream(path);
}
})
.listen(STREAM_PORT);
console.log(
"Listening for incomming MPEG-TS Stream on http://127.0.0.1:" +
STREAM_PORT +
"/<secret>"
);
console.log(
"Awaiting WebSocket connections on ws://127.0.0.1:" + WEBSOCKET_PORT + "/"
);
// Run ffmpeg to stream data
const stream = spawn(STREAM_COMMAND, FFMPEG_ARGS.split(" "));
stream.stdout.on("data", data => {
console.log(`stream stdout: ${data}`);
});
stream.stderr.on("data", data => {
// console.error(`stream stderr: ${data}`);
});
stream.on("close", code => {
console.log(`stream child process exited with code ${code}`);
});