add jsmpeg relay

This commit is contained in:
Blake Blackshear 2021-03-02 06:54:12 -06:00
parent c71b717a54
commit 7309c06be8
6 changed files with 106 additions and 0 deletions

11
docker/Dockerfile.jsmpeg Normal file
View File

@ -0,0 +1,11 @@
ARG NODE_VERSION=14.16
FROM node:${NODE_VERSION}
WORKDIR /opt/build
COPY jsmpeg/. .
RUN npm install --no-progress && \
npm run build && \
npm prune --production --ignore-scripts --prefer-offline

1
jsmpeg/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

13
jsmpeg/package-lock.json generated Normal file
View File

@ -0,0 +1,13 @@
{
"name": "jsmpeg",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"ws": {
"version": "7.4.3",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.4.3.tgz",
"integrity": "sha512-hr6vCR76GsossIRsr8OLR9acVVm1jyfEWvhbNjtgPOrfvAlKzvyeg/P6r8RuDjRyrcQoPQT7K0DGEPc7Ae6jzA=="
}
}
}

14
jsmpeg/package.json Normal file
View File

@ -0,0 +1,14 @@
{
"name": "jsmpeg",
"version": "1.0.0",
"description": "",
"main": "relay.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"dependencies": {
"ws": "^7.4.3"
}
}

54
jsmpeg/relay.js Normal file
View File

@ -0,0 +1,54 @@
// 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 relay 8081 8082
// ffmpeg -i <some input> -f mpegts http://localhost:8081/streamName
var http = require('http'),
WebSocket = require('ws');
var STREAM_PORT = process.argv[2] || 8081,
WEBSOCKET_PORT = process.argv[3] || 8082;
// Websocket Server
var socketServer = new WebSocket.Server({ port: WEBSOCKET_PORT, perMessageDeflate: false });
socketServer.on('connection', function (socket, request) {
var streamName = request.url.substr(1).split('/')[0];
socket.stream = streamName;
console.log(
'New WebSocket connection for: ',
streamName
);
});
socketServer.broadcast = function(data, streamName) {
socketServer.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN && client.stream === streamName) {
client.send(data);
}
});
};
// HTTP Server to accept incoming MPEG-TS Stream from ffmpeg
var streamServer = http.createServer( function(request, response) {
var streamName = request.url.substr(1).split('/')[0];
response.connection.setTimeout(0);
console.log(
'Stream Connected: ',
streamName
);
request.on('data', function(data){
socketServer.broadcast(data, streamName);
});
})
// Keep the socket open for streaming
streamServer.headersTimeout = 0;
streamServer.listen(STREAM_PORT);
console.log('Listening for incoming MPEG-TS Stream on http://127.0.0.1:'+STREAM_PORT+'/<streamName>');
console.log('Awaiting WebSocket connections on ws://127.0.0.1:'+WEBSOCKET_PORT+'/');

View File

@ -32,6 +32,11 @@ http {
keepalive 1024;
}
upstream jsmpeg {
server localhost:8082;
keepalive 1024;
}
server {
listen 5000;
@ -140,6 +145,14 @@ http {
proxy_set_header Host $host;
}
location /live/ {
proxy_pass http://jsmpeg/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
location /api/ {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';