2019-09-20 11:09:15 -05:00
|
|
|
import * as NodeMediaServer from "node-media-server";
|
2019-11-27 22:18:55 -06:00
|
|
|
import * as dirty from "dirty";
|
2019-11-16 11:34:16 -06:00
|
|
|
import { mkdir, fstat, access } from "fs";
|
2019-11-16 14:42:26 -06:00
|
|
|
import * as strf from "strftime";
|
2019-09-22 16:33:18 -05:00
|
|
|
import * as db from "./database";
|
2019-11-16 11:34:16 -06:00
|
|
|
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
const { exec, execFile } = require('child_process');
|
2019-09-11 19:28:17 -05:00
|
|
|
|
2019-11-27 22:18:55 -06:00
|
|
|
const keystore = dirty();
|
|
|
|
|
2019-09-26 14:43:24 -05:00
|
|
|
function init (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) => {
|
2019-10-05 14:34:57 -05:00
|
|
|
console.log("[NodeMediaServer] Publish 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
|
2019-11-27 22:18:55 -06:00
|
|
|
if (StreamPath.split("/").length !== 3 || key.includes(' ')){
|
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-11-27 22:18:55 -06:00
|
|
|
/*if(app === satyrconfig.publicEndpoint) {
|
2019-11-24 20:17:15 -06:00
|
|
|
if(session.isLocal) {
|
2019-09-24 17:29:37 -05:00
|
|
|
//only allow publish to public endpoint from localhost
|
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-10-05 14:34:57 -05:00
|
|
|
//set live flag
|
|
|
|
db.query('update user_meta set live=true where username=\''+key+'\' limit 1');
|
2019-09-24 17:29:37 -05:00
|
|
|
//if this stream is from the public endpoint, check if we should be recording
|
2019-09-28 21:43:25 -05:00
|
|
|
return db.query('select username,record_flag from users where username=\''+key+'\' limit 1').then((results) => {
|
|
|
|
if(results[0].record_flag && satyrconfig.record){
|
2019-09-22 16:33:18 -05:00
|
|
|
console.log('[NodeMediaServer] Initiating recording for stream:',id);
|
2019-11-16 11:34:16 -06:00
|
|
|
mkdir(satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, { recursive : true }, (err) => {
|
2019-09-22 16:33:18 -05:00
|
|
|
if (err) throw err;
|
2019-11-24 20:17:15 -06:00
|
|
|
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'], {
|
2019-09-23 14:27:01 -05:00
|
|
|
detached : true,
|
2019-11-24 20:17:15 -06:00
|
|
|
stdio : 'inherit',
|
|
|
|
maxBuffer: Infinity
|
|
|
|
}).unref();
|
2019-09-23 14:27:01 -05:00
|
|
|
//spawn an ffmpeg process to record the stream, then detach it completely
|
2019-11-16 11:34:16 -06:00
|
|
|
//ffmpeg can then (probably) 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-11-27 22:18:55 -06: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-11-27 22:18:55 -06:00
|
|
|
db.query('select username,record_flag from users where stream_key='+db.raw.escape(key)+' limit 1').then(async (results) => {
|
2019-09-22 16:33:18 -05:00
|
|
|
if(results[0]){
|
2019-11-16 11:34:16 -06:00
|
|
|
//push to rtmp
|
2019-11-27 22:18:55 -06:00
|
|
|
//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});
|
2019-11-16 11:34:16 -06:00
|
|
|
//push to mpd after making sure directory exists
|
2019-11-27 22:18:55 -06:00
|
|
|
keystore[results[0].username] = key;
|
2019-11-25 09:36:50 -06:00
|
|
|
mkdir(satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, { recursive : true }, async (err) => {
|
|
|
|
if (err) throw err;
|
|
|
|
while(true){
|
|
|
|
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});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
await sleep(300);
|
2019-11-24 20:17:15 -06:00
|
|
|
}
|
2019-11-25 09:36:50 -06:00
|
|
|
});
|
2019-11-27 22:18:55 -06:00
|
|
|
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.prviateEndpoint+'/'+key, '-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);
|
|
|
|
}
|
|
|
|
db.query('update user_meta set live=true where username=\''+results[0].username+'\' limit 1');
|
|
|
|
console.log('[NodeMediaServer] Stream key ok for stream:',id);
|
2019-09-22 16:33:18 -05:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
console.log('[NodeMediaServer] Invalid stream key for stream:',id);
|
|
|
|
session.reject();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-10-05 14:34:57 -05:00
|
|
|
nms.on('donePublish', (id, StreamPath, args) => {
|
|
|
|
let app: string = StreamPath.split("/")[1];
|
|
|
|
let key: string = StreamPath.split("/")[2];
|
2019-11-27 22:18:55 -06:00
|
|
|
if(app === satyrconfig.privateEndpoint) {
|
|
|
|
db.query('update user_meta,users set user_meta.live=false where users.stream_key='+db.raw.escape(key));
|
|
|
|
db.query('select username from users where stream_key='+db.raw.escape(key)+' limit 1').then(async (results) => {
|
|
|
|
keystore.rm(results[0].username);
|
|
|
|
});
|
|
|
|
|
2019-10-05 14:34:57 -05:00
|
|
|
}
|
|
|
|
});
|
2019-09-22 16:33:18 -05:00
|
|
|
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-11-24 20:17:15 -06:00
|
|
|
if(app === satyrconfig.privateEndpoint && !session.isLocal) {
|
|
|
|
console.log("[NodeMediaServer] Non-local Play from private endpoint, rejecting client:",id);
|
|
|
|
session.reject();
|
2019-11-27 22:18:55 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(app === satyrconfig.publicEndpoint) {
|
|
|
|
if(keystore[key]){
|
|
|
|
session.playStreamPath = '/'+satyrconfig.privateEndpoint+'/'+keystore[key];
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-20 11:09:15 -05:00
|
|
|
}
|
2019-11-27 22:18:55 -06:00
|
|
|
session.reject();
|
2019-09-20 11:09:15 -05:00
|
|
|
});
|
|
|
|
}
|
2019-09-26 14:43:24 -05:00
|
|
|
export { init };
|