Fix a bug where we treated local clients the same as foreign ones

merge-requests/5/head
knotteye 2019-11-30 14:15:31 -06:00
parent d4f92c33ff
commit 31426a0c41
1 changed files with 12 additions and 47 deletions

View File

@ -23,40 +23,6 @@ function init (mediaconfig: any, satyrconfig: any) {
session.reject(); session.reject();
return false; return false;
} }
/*if(app === satyrconfig.publicEndpoint) {
if(session.isLocal) {
//only allow publish to public endpoint from localhost
console.log("[NodeMediaServer] Local publish, stream:",`${id} ok.`);
}
else{
console.log("[NodeMediaServer] Non-local Publish to public endpoint, rejecting stream:",id);
session.reject();
return false;
}
console.log("[NodeMediaServer] Public endpoint, checking record flag.");
//set live flag
db.query('update user_meta set live=true where username=\''+key+'\' limit 1');
//if this stream is from the public endpoint, check if we should be recording
return db.query('select username,record_flag from users where username=\''+key+'\' limit 1').then((results) => {
if(results[0].record_flag && satyrconfig.record){
console.log('[NodeMediaServer] Initiating recording for stream:',id);
mkdir(satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, { recursive : true }, (err) => {
if (err) throw err;
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,
stdio : 'inherit',
maxBuffer: Infinity
}).unref();
//spawn an ffmpeg process to record the stream, then detach it completely
//ffmpeg can then (probably) finalize the recording if satyr crashes mid-stream
});
}
else {
console.log('[NodeMediaServer] Skipping recording for stream:',id);
}
return true;
});
}*/
if(app !== satyrconfig.privateEndpoint){ if(app !== satyrconfig.privateEndpoint){
//app isn't at public endpoint if we've reached this point //app isn't at public endpoint if we've reached this point
console.log("[NodeMediaServer] Wrong endpoint, rejecting stream:",id); console.log("[NodeMediaServer] Wrong endpoint, rejecting stream:",id);
@ -72,16 +38,14 @@ function init (mediaconfig: any, satyrconfig: any) {
//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}); //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});
//push to mpd after making sure directory exists //push to mpd after making sure directory exists
keystore[results[0].username] = key; keystore[results[0].username] = key;
mkdir(satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, { recursive : true }, async (err) => { mkdir(satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, { recursive : true }, ()=>{;});
if (err) throw err; while(true){
while(true){ if(session.audioCodec !== 0 && session.videoCodec !== 0){
if(session.audioCodec !== 0 && session.videoCodec !== 0){ 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});
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}); break;
break;
}
await sleep(300);
} }
}); await sleep(300);
}
if(results[0].record_flag && satyrconfig.record){ if(results[0].record_flag && satyrconfig.record){
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) => {
@ -128,20 +92,21 @@ function init (mediaconfig: any, satyrconfig: any) {
session.reject(); session.reject();
return false; return false;
} }
//disallow playing from the private endpoint for anyone except localhost //localhost can play from whatever endpoint
//(this will be the ffmpeg instance redirecting the stream) //other clients must use private endpoint
if(app === satyrconfig.privateEndpoint && !session.isLocal) { if(app !== satyrconfig.publicEndpoint && !session.isLocal) {
console.log("[NodeMediaServer] Non-local Play from private endpoint, rejecting client:",id); console.log("[NodeMediaServer] Non-local Play from private endpoint, rejecting client:",id);
session.reject(); session.reject();
return false; return false;
} }
//rewrite playpath to private endpoint serverside
//(hopefully)
if(app === satyrconfig.publicEndpoint) { if(app === satyrconfig.publicEndpoint) {
if(keystore[key]){ if(keystore[key]){
session.playStreamPath = '/'+satyrconfig.privateEndpoint+'/'+keystore[key]; session.playStreamPath = '/'+satyrconfig.privateEndpoint+'/'+keystore[key];
return true; return true;
} }
} }
session.reject();
}); });
} }
export { init }; export { init };