2019-09-20 11:09:15 -05:00
|
|
|
import * as NodeMediaServer from "node-media-server";
|
|
|
|
import { mkdir } from "fs";
|
2019-09-22 16:33:18 -05:00
|
|
|
import * as db from "./database";
|
2019-09-14 21:22:07 -05:00
|
|
|
const { exec } = require('child_process');
|
2019-09-11 19:28:17 -05:00
|
|
|
|
2019-09-23 14:27:01 -05:00
|
|
|
function boot (mediaconfig: any, satyrconfig: any) {
|
2019-09-22 16:33:18 -05:00
|
|
|
const nms = new NodeMediaServer(mediaconfig);
|
2019-09-20 11:09:15 -05:00
|
|
|
nms.run();
|
2019-09-11 19:28:17 -05:00
|
|
|
|
2019-09-22 16:33:18 -05:00
|
|
|
nms.on('postPublish', (id, StreamPath, args) => {
|
|
|
|
console.log("[NodeMediaServer] Prepublish Hook for stream:",id);
|
2019-09-20 11:09:15 -05:00
|
|
|
let session = nms.getSession(id);
|
2019-09-22 16:33:18 -05:00
|
|
|
let app: string = StreamPath.split("/")[1];
|
|
|
|
let key: string = StreamPath.split("/")[2];
|
2019-09-23 15:59:07 -05:00
|
|
|
//disallow urls not formatted exactly right
|
|
|
|
if (StreamPath.split("/").length !== 3){
|
2019-09-22 16:33:18 -05:00
|
|
|
console.log("[NodeMediaServer] Malformed URL, closing connection for stream:",id);
|
2019-09-20 11:09:15 -05:00
|
|
|
session.reject();
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-23 15:59:07 -05:00
|
|
|
if(app === mediaconfig.trans.tasks[0].app) {
|
|
|
|
if(session.ip.includes('127.0.0.1') || session.ip === '::1') {
|
2019-09-24 17:29:37 -05:00
|
|
|
//only allow publish to public endpoint from localhost
|
|
|
|
//this is NOT a comprehensive way of doing this, but I'm ignoring it
|
|
|
|
//until satyr releases and someone opens an issue
|
2019-09-23 15:59:07 -05:00
|
|
|
console.log("[NodeMediaServer] Local publish, stream:",`${id} ok.`);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
console.log("[NodeMediaServer] Non-local Publish to public endpoint, rejecting stream:",id);
|
|
|
|
session.reject();
|
2019-09-24 17:29:37 -05:00
|
|
|
return false;
|
2019-09-23 15:59:07 -05:00
|
|
|
}
|
2019-09-22 16:33:18 -05:00
|
|
|
console.log("[NodeMediaServer] Public endpoint, checking record flag.");
|
2019-09-24 17:29:37 -05:00
|
|
|
//if this stream is from the public endpoint, check if we should be recording
|
|
|
|
return db.query('select username from users where username=\''+key+'\' and record_flag=true limit 1').then((results) => {
|
2019-09-23 15:59:07 -05:00
|
|
|
if(results[0].username && satyrconfig.record){
|
2019-09-22 16:33:18 -05:00
|
|
|
console.log('[NodeMediaServer] Initiating recording for stream:',id);
|
2019-09-23 15:59:07 -05:00
|
|
|
mkdir(mediaconfig.http.mediaroot+'/'+mediaconfig.trans.tasks[0].app+'/'+results[0].username, { recursive : true }, (err) => {
|
2019-09-22 16:33:18 -05:00
|
|
|
if (err) throw err;
|
2019-09-23 15:59:07 -05:00
|
|
|
let subprocess = exec('ffmpeg -i rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+mediaconfig.trans.tasks[0].app+'/'+results[0].username+' -vcodec copy -acodec copy '+mediaconfig.http.mediaroot+'/'+mediaconfig.trans.tasks[0].app+'/'+results[0].username+'/$(date +%d%b%Y-%H%M).mp4',{
|
2019-09-23 14:27:01 -05:00
|
|
|
detached : true,
|
|
|
|
stdio : 'inherit'
|
|
|
|
});
|
|
|
|
subprocess.unref();
|
|
|
|
//spawn an ffmpeg process to record the stream, then detach it completely
|
2019-09-23 15:59:07 -05:00
|
|
|
//ffmpeg can then finalize the recording if satyr crashes mid-stream
|
2019-09-22 16:33:18 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log('[NodeMediaServer] Skipping recording for stream:',id);
|
|
|
|
}
|
2019-09-24 17:29:37 -05:00
|
|
|
return true;
|
2019-09-22 16:33:18 -05:00
|
|
|
});
|
|
|
|
}
|
2019-09-23 14:27:01 -05:00
|
|
|
if(app !== satyrconfig.privateEndpoint){
|
|
|
|
//app isn't at public endpoint if we've reached this point
|
2019-09-22 16:33:18 -05:00
|
|
|
console.log("[NodeMediaServer] Wrong endpoint, rejecting stream:",id);
|
2019-09-20 11:09:15 -05:00
|
|
|
session.reject();
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-23 15:59:07 -05:00
|
|
|
//if the url is formatted correctly and the user is streaming to the correct private endpoint
|
|
|
|
//grab the username from the database and redirect the stream there if the key is valid
|
|
|
|
//otherwise kill the session
|
2019-09-24 17:29:37 -05:00
|
|
|
db.query('select username from users where stream_key=\''+key+'\' limit 1').then((results) => {
|
2019-09-22 16:33:18 -05:00
|
|
|
if(results[0]){
|
2019-09-23 15:59:07 -05:00
|
|
|
exec('ffmpeg -analyzeduration 0 -i rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+satyrconfig.privateEndpoint+'/'+key+' -vcodec copy -acodec copy -crf 18 -f flv rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+mediaconfig.trans.tasks[0].app+'/'+results[0].username);
|
2019-09-22 16:33:18 -05:00
|
|
|
console.log('[NodeMediaServer] Stream key okay for stream:',id);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
console.log('[NodeMediaServer] Invalid stream key for stream:',id);
|
|
|
|
session.reject();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
nms.on('prePlay', (id, StreamPath, args) => {
|
|
|
|
let session = nms.getSession(id);
|
|
|
|
let app: string = StreamPath.split("/")[1];
|
|
|
|
let key: string = StreamPath.split("/")[2];
|
2019-09-23 15:59:07 -05:00
|
|
|
//correctly formatted urls again
|
|
|
|
if (StreamPath.split("/").length !== 3){
|
2019-09-22 16:33:18 -05:00
|
|
|
console.log("[NodeMediaServer] Malformed URL, closing connection for stream:",id);
|
2019-09-20 11:09:15 -05:00
|
|
|
session.reject();
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-23 15:59:07 -05:00
|
|
|
//disallow playing from the private endpoint for anyone except localhost
|
|
|
|
//(this will be the ffmpeg instance redirecting the stream)
|
2019-09-23 14:27:01 -05:00
|
|
|
if(app === satyrconfig.privateEndpoint) {
|
2019-09-23 15:59:07 -05:00
|
|
|
if(session.ip.includes('127.0.0.1') || session.ip === '::1') {
|
|
|
|
console.log("[NodeMediaServer] Local play, client:",`${id} ok.`);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
console.log("[NodeMediaServer] Non-local Play from private endpoint, rejecting client:",id);
|
|
|
|
session.reject();
|
|
|
|
}
|
2019-09-20 11:09:15 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
export { boot };
|