Fix a bug where streams would crash upon FFMPEG exceeding maximum child_process buffer

Change scripts to automatically build before serving
Move typescript to dependencies
Check if the stream is ready to transcode to MPEG-DASH programmatically instead of waiting for 5 seconds
Use session.isLocal instead of matching session.ip against some strings
(session.isLocal does that internally but at least this way looks prettier)
merge-requests/4/head
knotteye 2019-11-24 20:17:15 -06:00
parent 239cd252a2
commit 1f3504f698
4 changed files with 22 additions and 28 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "satyr", "name": "satyr",
"version": "0.4.1", "version": "0.4.2",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -5,8 +5,7 @@
"license": "AGPL-3.0", "license": "AGPL-3.0",
"author": "knotteye", "author": "knotteye",
"scripts": { "scripts": {
"start": "node build/controller.js", "start": "tsc && node build/controller.js",
"build": "tsc",
"user": "node build/cli.js", "user": "node build/cli.js",
"setup": "sh install/setup.sh" "setup": "sh install/setup.sh"
}, },
@ -27,10 +26,10 @@
"recursive-readdir": "^2.2.2", "recursive-readdir": "^2.2.2",
"socket.io": "^2.3.0", "socket.io": "^2.3.0",
"strftime": "^0.10.0", "strftime": "^0.10.0",
"toml": "^3.0.0" "toml": "^3.0.0",
"typescript": "^3.6.3"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^12.7.5", "@types/node": "^12.7.5"
"typescript": "^3.6.3"
} }
} }

View File

@ -61,6 +61,7 @@ async function run() {
api.init(satyr); api.init(satyr);
http.init(satyr, config.server.http.port, config.ircd); http.init(satyr, config.server.http.port, config.ircd);
mediaserver.init(nms, satyr); mediaserver.init(nms, satyr);
console.log(`Satyr v${process.env.npm_package_version} ready`);
} }
run(); run();
export { run }; export { run };

View File

@ -21,10 +21,8 @@ function init (mediaconfig: any, satyrconfig: any) {
return false; return false;
} }
if(app === satyrconfig.publicEndpoint) { if(app === satyrconfig.publicEndpoint) {
if(session.ip.includes('127.0.0.1') || session.ip === '::1') { if(session.isLocal) {
//only allow publish to public endpoint from localhost //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
console.log("[NodeMediaServer] Local publish, stream:",`${id} ok.`); console.log("[NodeMediaServer] Local publish, stream:",`${id} ok.`);
} }
else{ else{
@ -41,11 +39,11 @@ function init (mediaconfig: any, satyrconfig: any) {
console.log('[NodeMediaServer] Initiating recording for stream:',id); console.log('[NodeMediaServer] Initiating recording for stream:',id);
mkdir(satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, { recursive : true }, (err) => { mkdir(satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, { recursive : true }, (err) => {
if (err) throw err; if (err) throw err;
let subprocess = execFile(satyrconfig.ffmpeg, ['-i', 'rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, '-vcodec', 'copy', '-acodec', 'copy', satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username+'/'+strf('%d%b%Y-%H%M')+'.mp4'], { execFile(satyrconfig.ffmpeg, ['-loglevel', 'fatal', '-i', 'rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, '-vcodec', 'copy', '-acodec', 'copy', satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username+'/'+strf('%d%b%Y-%H%M')+'.mp4'], {
detached : true, detached : true,
stdio : 'inherit' stdio : 'inherit',
}); maxBuffer: Infinity
subprocess.unref(); }).unref();
//spawn an ffmpeg process to record the stream, then detach it completely //spawn an ffmpeg process to record the stream, then detach it completely
//ffmpeg can then (probably) finalize the recording if satyr crashes mid-stream //ffmpeg can then (probably) finalize the recording if satyr crashes mid-stream
}); });
@ -72,16 +70,17 @@ function init (mediaconfig: any, satyrconfig: any) {
db.query('select username from users where stream_key='+db.raw.escape(key)+' limit 1').then(async (results) => { db.query('select username from users where stream_key='+db.raw.escape(key)+' limit 1').then(async (results) => {
if(results[0]){ if(results[0]){
//push to rtmp //push to rtmp
execFile(satyrconfig.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+'/'+satyrconfig.publicEndpoint+'/'+results[0].username]); execFile(satyrconfig.ffmpeg, ['-loglevel', 'fatal', '-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+'/'+satyrconfig.publicEndpoint+'/'+results[0].username], {maxBuffer: Infinity});
//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+'/'+satyrconfig.publicEndpoint+'/'+results[0].username); //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+'/'+satyrconfig.publicEndpoint+'/'+results[0].username);
//push to mpd after making sure directory exists //push to mpd after making sure directory exists
mkdir(satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, { recursive : true }, (err) => {;}); mkdir(satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, { recursive : true }, (err) => {;});
sleep(5000).then( () => { while(true){
//wait for stream to initialize, but i'm not happy about this if(session.audioCodec !== 0 && session.videoCodec !== 0){
execFile(satyrconfig.ffmpeg, ['-y', '-i', 'rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+satyrconfig.privateEndpoint+'/'+key, '-map', '0:2', '-map', '0:2', '-map', '0:2', '-map', '0:1', '-c:a', 'copy', '-c:v:0', 'copy', '-c:v:1', 'libx264', '-c:v:2', 'libx264', '-crf:1', '33', '-crf:2', '40', '-b:v:1', '3000K', '-b:v:2', '1500K', '-remove_at_exit', '1', '-seg_duration', '1', '-window_size', '30', '-f', 'dash', satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username+'/index.mpd']); execFile(satyrconfig.ffmpeg, ['-loglevel', 'fatal', '-y', '-i', 'rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+satyrconfig.privateEndpoint+'/'+key, '-map', '0:2', '-map', '0:2', '-map', '0:2', '-map', '0:1', '-c:a', 'copy', '-c:v:0', 'copy', '-c:v:1', 'libx264', '-c:v:2', 'libx264', '-crf:1', '33', '-crf:2', '40', '-b:v:1', '3000K', '-b:v:2', '1500K', '-remove_at_exit', '1', '-seg_duration', '1', '-window_size', '30', '-f', 'dash', satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username+'/index.mpd'], {maxBuffer: Infinity});
//exec('ffmpeg -y -i rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+satyrconfig.privateEndpoint+'/'+key+' -map 0:2 -map 0:2 -map 0:2 -map 0:1 -c:a copy -c:v:0 copy -c:v:1 libx264 -c:v:2 libx264 -crf:1 33 -crf:2 40 -b:v:1 3000K -b:v:2 1500K -remove_at_exit 1 -seg_duration 1 -window_size 30 -f dash '+satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username+'/index.mpd'); break;
}); }
console.log('[NodeMediaServer] Stream key okay for stream:',id); await sleep(300);
}
} }
else{ else{
console.log('[NodeMediaServer] Invalid stream key for stream:',id); console.log('[NodeMediaServer] Invalid stream key for stream:',id);
@ -108,14 +107,9 @@ function init (mediaconfig: any, satyrconfig: any) {
} }
//disallow playing from the private endpoint for anyone except localhost //disallow playing from the private endpoint for anyone except localhost
//(this will be the ffmpeg instance redirecting the stream) //(this will be the ffmpeg instance redirecting the stream)
if(app === satyrconfig.privateEndpoint) { if(app === satyrconfig.privateEndpoint && !session.isLocal) {
if(session.ip.includes('127.0.0.1') || session.ip === '::1') { console.log("[NodeMediaServer] Non-local Play from private endpoint, rejecting client:",id);
console.log("[NodeMediaServer] Local play, client:",`${id} ok.`); session.reject();
}
else{
console.log("[NodeMediaServer] Non-local Play from private endpoint, rejecting client:",id);
session.reject();
}
} }
}); });
} }